Integrating an SMS gateway into your C program allows you to send real-time notifications, OTPs, and alerts directly from your applications. In this guide, weโll walk through how to add SMS Gateways for my C Program using Text.lk API.
๐ง Prerequisites #
Before you begin:
- Text.lk account โ Sign up and get your API Key from the dashboard.
- C compiler (like gcc).
- Installed libcurl (for the recommended method).
sudo apt-get install curl- Installed curl (for the simplest method).
sudo apt-get install libcurl4-openssl-dev๐ Method 1: Recommended Way (Using libcurl) #
If you need more control (like checking if SMS was delivered), use libcurl inside your C program.
โ Example Code (Method 1) #
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
int main(void) {
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
const char *url = "https://app.text.lk/api/v3/sms/send";
const char *data = "{"
"\"recipient\":\"94710000000\","
"\"sender_id\":\"TextLKDemo\","
"\"type\":\"plain\","
"\"message\":\"This is a test message\""
"}";
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Authorization: Bearer API_KEY");
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, "Accept: application/json");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
res = curl_easy_perform(curl);
if(res != CURLE_OK) {
fprintf(stderr, "Request failed: %s\n", curl_easy_strerror(res));
} else {
printf("SMS request sent successfully!\n");
}
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
return 0;
}โก How to Compile & Run (Method 1) #
gcc send_sms.c -o send_sms -lcurl
./send_smsโ Youโll see the SMS request sent successfully! If everything works.
โ Pros & Cons (Method 1) #
- โ More reliable and professional.
- โ Can handle response (e.g., success/failure, delivery ID).
- โ Slightly more code to write.
- โ Needs libcurl library installed.
๐ Method 2: The Simplest Way (Using system() + curl) #
If you just want to send SMS with minimum code, you can run the curl command directly from your C program.
โ Example Code (Method 2) #
#include <stdlib.h>
int main() {
system("curl -X POST https://app.text.lk/api/v3/sms/send "
"-H 'Authorization: Bearer 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\"}'");
return 0;
}โก How to Compile & Run (Method 2) #
- Save file as send_sms.c
- Compile:
gcc send_sms.c -o send_sms- Run:
./send_smsโ Thatโs it โ your SMS will be sent!
โ Pros & Cons (Method 2) #
- โ Very easy (just a few lines of code).
- โ No extra libraries needed.
- โ You canโt easily check the response in C.
- โ Depends on curl being installed.
๐ Replace with Your Own Details #
- API_KEY โ Your Text.lk API Key
- 94710000000 โ Recipientโs mobile number (with country code)
- TextLKDemo โ Your approved sender ID
- “This is a test message” โ Your SMS text
๐ฏ Conclusion #
- If you need response handling & reliability โ Use Method 1 with libcurl.
- If you need quick & simple SMS sending โ Use Method 2 with system() + curl.
With either method, youโve now successfully integrated the Text.lk SMS Gateway into your C program! ๐