Text.lk supports two main API methods (Bearer JSON POST and HTTP GET); here’s a complete developer guide for integrating SMS sending in iOS using Swift with both methods.
📱 Send SMS Using Text.lk API in iOS (Swift) — Full Guide #
Text.lk provides two methods for sending SMS:
- Method 1: JSON POST request with Bearer token (recommended)
- Method 2: Simple GET request using query parameters
This guide covers both methods, complete with sample Swift code.
🧩 Requirements #
- iOS 13.0 or later (Swift 5+)
- A valid Text.lk account with an API key and approved sender ID
- Internet permission (default in iOS apps)
⚙️ Method 1: JSON POST (Bearer Token Authentication) #
This method is more secure and flexible, ideal for production use.
✅ cURL Example #
curl -X POST https://app.text.lk/api/v3/sms/send \
-H 'Authorization: Bearer YOUR_TEXTLK_API_KEY' \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-d '{
"recipient": "94710000000",
"sender_id": "TextLKDemo",
"type": "plain",
"message": "This is a test message"
}'📱 Swift Example (Method 1) #
import UIKit
class SMSViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
sendSMSBearer()
}
func sendSMSBearer() {
let apiKey = "YOUR_TEXTLK_API_KEY"
let url = URL(string: "https://app.text.lk/api/v3/sms/send")!
let parameters: [String: Any] = [
"recipient": "94710000000",
"sender_id": "TextLKDemo",
"type": "plain",
"message": "Hello from Swift using Text.lk!"
]
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")
request.setValue("Bearer \(apiKey)", forHTTPHeaderField: "Authorization")
request.httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: [])
let task = URLSession.shared.dataTask(with: request) { data, response, error in
if let error = error {
print("❌ Error:", error.localizedDescription)
return
}
guard let data = data else { return }
do {
let json = try JSONSerialization.jsonObject(with: data, options: [])
print("✅ SMS Sent Response:", json)
} catch {
print("❌ JSON Parse Error:", error)
}
}
task.resume()
}
}
⚙️ Method 2: HTTP GET (Query Parameters) #
This method is simpler and can be used for quick tests or lightweight integrations.
✅ cURL Example #
curl "https://app.text.lk/api/http/sms/send?recipient=94710000000&sender_id=TextLKDemo&message=This%20is%20a%20test%20message&api_token=YOUR_TEXTLK_API_KEY"📱 Swift Example (Method 2) #
import UIKit
class SimpleSMSViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
sendSMSHTTP()
}
func sendSMSHTTP() {
let apiKey = "YOUR_TEXTLK_API_KEY"
let recipient = "94710000000"
let senderID = "TextLKDemo"
let message = "Hello from Swift using HTTP method!"
// Encode message to make it URL-safe
let encodedMessage = message.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? message
let urlString = "https://app.text.lk/api/http/sms/send?recipient=\(recipient)&sender_id=\(senderID)&message=\(encodedMessage)&api_token=\(apiKey)"
guard let url = URL(string: urlString) else { return }
let task = URLSession.shared.dataTask(with: url) { data, response, error in
if let error = error {
print("❌ Error:", error.localizedDescription)
return
}
guard let data = data else { return }
do {
let json = try JSONSerialization.jsonObject(with: data, options: [])
print("✅ SMS Sent Response:", json)
} catch {
print("❌ JSON Parse Error:", error)
}
}
task.resume()
}
}
🔒 Security Tips #
- Store your API key securely — avoid hardcoding it in the app.
Use Keychain or fetch it from a secure backend. - Always use HTTPS endpoints.
- Use Method 1 (Bearer token) for all real-world apps — it’s more secure and supports additional fields.
🧠 Summary #
| Feature | Method 1 (Bearer JSON POST) | Method 2 (HTTP GET) |
|---|---|---|
| Security | ✅ High (Bearer Token) | ⚠️ Lower (URL-based) |
| Ease of Use | Medium | Easy |
| Supports JSON Body | ✅ Yes | ❌ No |
| Recommended For | Production | Testing / Simple Scripts |
📘 Example Output #
Successful response (JSON):
{
"status": "success",
"message": "Message has been sent successfully.",
"data": {
"recipient": "94710000000",
"message_id": "abc123xyz",
}
}Failed response (JSON):
{
"status": "error",
"message": "Human readable error message.",
}