ES

C code examples for LabsMobile SMS API integration

Below are the examples of programming code in C language to send SMS messages through the LabsMobile platform API.

This documentation is designed for you to connect your applications with the LabsMobile platform and automate the sending of SMS messages. The main objective of the integration of these applications is the sending of SMS messages and related communications.

Advice We recommend using our API SMS http/POST in JSON format for any integration with the LabsMobile platform. But we have other API versions that you can use depending on your environment and requirements.

With these examples you will be able to perform the integration of the following functionalities:

  • Send SMS messages individually or in bulk.
  • Check your account balance.
  • Receive delivery confirmation and/or error notifications corresponding to the messages sent.
  • Obtain notification and data of the messages received in the virtual numbers contracted.

To be taken into account

The LabsMobile API SMS uses a common base URL for all requests: https://api.labsmobile.com.

It is highly recommended to use a URL that includes the HTTPS protocol in any version of the API.

Authentication is done with the username and an API token, myUsername:myToken. You must create the API token from the API Settings section of your account.


Send SMS with http/POST

Here is an example of code in C language to send SMS messages using the API SMS http/POST that uses the JSON format for the information exchange variables.

As you can see, you need to create a structure in JSON format containing all the necessary parameters for sending and make a HTTP/POST call with this data in the body of the request.

Simple sending to a single recipient with http/POST (C)
int main(void) {
   CURL *curl;
   CURLcode res;
   curl = curl_easy_init();
   if(curl) {
       curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
       curl_easy_setopt(curl, CURLOPT_URL, "https://api.labsmobile.com/json/send");
       curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
       curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
       curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_BASIC);
       curl_easy_setopt(curl, CURLOPT_USERNAME, "myUsername");
       curl_easy_setopt(curl, CURLOPT_PASSWORD, "myToken");
       struct curl_slist *headers = NULL;
       headers = curl_slist_append(headers, "Cache-Control: no-cache");
       headers = curl_slist_append(headers, "Content-Type: application/json");
       curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
       const char *data = "{\r\n  \"message\":\"Your verification code is 123\",\r\n  \"tpoa\":\"Sender\",\r\n  \"recipient\":\r\n    [\r\n      {\r\n        \"msisdn\":\"12015550123\"\r\n      }\r\n    ]\r\n}\r\n\r\n";
       curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
       res = curl_easy_perform(curl);
       if(res != CURLE_OK)
           fprintf(stderr, "curl_easy_perform() failed: %s\n",
                     curl_easy_strerror(res));
       curl_easy_cleanup(curl);
   }
   return 0;
}
              

Bulk sending to multiple recipients with http/POST (C)
int main(void) {
   CURL *curl;
   CURLcode res;
curl = curl_easy_init();
if(curl) {
 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
 curl_easy_setopt(curl, CURLOPT_URL, "https://api.labsmobile.com/json/send");
 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
 curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
 curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_BASIC);
 curl_easy_setopt(curl, CURLOPT_USERNAME, "myUsername");
 curl_easy_setopt(curl, CURLOPT_PASSWORD, "myToken");
 struct curl_slist *headers = NULL;
 headers = curl_slist_append(headers, "Cache-Control: no-cache");
 headers = curl_slist_append(headers, "Content-Type: application/json");
 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
 const char *data = "{\r\n  \"message\":\"Don't miss our Sale! Use code XXXX123 for 20% off.\",\r\n  \"tpoa\":\"Sender\",\r\n  \"recipient\":\r\n    [\r\n      {\r\n        \"msisdn\":\"12015550123\"\r\n      },\r\n      {\r\n        \"msisdn\":\"12015550124\"\r\n      },\r\n      {\r\n        \"msisdn\":\"12015550125\"\r\n      }\r\n    ]\r\n}\r\n\r\n";
 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
 res = curl_easy_perform(curl);
 if(res != CURLE_OK)
   fprintf(stderr, "curl_easy_perform() failed: %s\n",
      curl_easy_strerror(res));
   curl_easy_cleanup(curl);
  }
 return 0;
}
                

Positive result
{
  "subid": "65f33a88ceb3d",
  "code": "0",
  "message": "Message has been successfully sent."
}
  

Wrong result
{
  "subid": "65f7f7041385d",
  "code": "35",
  "message": "The account has no enough credit for this sending"
}
  

All error codes can be found in API documentation, error codes.

For more details on the available parameters and configuration options, see the official documentation at:


Send SMS with http/GET

This is an example of code in C language to send SMS messages using the SMS API http/GET.

As you will see, you must pass a series of GET variables in the same URL and make a HTTP call. It is important to encode all values as URL using the curl_easy_escape() function.

Important The SMS http/GET API transmits credentials (username and API token) unencrypted and unsecured. We recommend using this API GET only when absolutely essential and use the API SMS http/POST instead.

Simple sending to a single recipient with http/GET (C)
int main(void)
{
   CURL *curl;
   CURLcode res;

   char *username = "myUsername";
   char *token = "myToken";
   char *msisdn = "12015550123";
   char *message = "Your verification code is 123";

   char *encoded_username = curl_easy_escape(curl, username, 0);
   char *encoded_message = curl_easy_escape(curl, message, 0);

   char url[500];
   sprintf(url, "https://api.labsmobile.com/get/send.php?username=%s&password=%s&msisdn=%s&message=%s", encoded_username, token, msisdn, encoded_message);

   curl = curl_easy_init();
   if (curl)
   {
       curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
       curl_easy_setopt(curl, CURLOPT_URL, url);
       curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
       curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
       res = curl_easy_perform(curl);
       if (res != CURLE_OK)
       {
           fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
       }
   }
   curl_free(encoded_username);
   curl_free(encoded_message);
   curl_easy_cleanup(curl);
   return 0;
}

                

Bulk sending to multiple recipients with http/GET (C)
int main(void)
{
   CURL *curl;
   CURLcode res;
   char *username = "myUsername";
   char *token = "myToken";
   char *msisdn = "[12015550123,12015550124,12015550125]";
   char *message = "Don't miss our Sale! Use code XXXX123 for 20% off."

   char *encoded_username = curl_easy_escape(curl, username, 0);
   char *encoded_message = curl_easy_escape(curl, message, 0);
   char *encoded_msisdn = curl_easy_escape(curl, msisdn, 0);

   char url[500];
   sprintf(url, "https://api.labsmobile.com/get/send.php?username=%s&password=%s&msisdn=%s&message=%s", encoded_username, token, encoded_msisdn, encoded_message);

   curl = curl_easy_init();
   if (curl)
   {
       curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
       curl_easy_setopt(curl, CURLOPT_URL, url);
       curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
       curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
       res = curl_easy_perform(curl);
       if (res != CURLE_OK)
       {
           fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
       }
   }
   curl_free(encoded_username);
   curl_free(encoded_message);
   curl_free(encoded_msisdn);
   curl_easy_cleanup(curl);
   return 0;
}
                

Positive result
{
  "subid": "65f33a88ceb3d",
  "code": "0",
  "message": "Message has been successfully sent."
}
  

Wrong result
{
  "subid": "65f7f7041385d",
  "code": "35",
  "message": "The account has no enough credit for this sending"
}
  

All error codes can be found in API documentation, error codes.

For more details on the available parameters and configuration options, see the official documentation at:


Account balance inquiry

With this code example in C you can consult your account balance using the LabsMobile API SMS.

Through a call to this endpoint, you can get information about the amount of credits available in your LabsMobile account. The connection is established through a HTTP/GET request with authentication in the HTTP connection header.

Account balance inquiry (C)
int main(void) {
   CURL *curl;
   CURLcode res;
   curl = curl_easy_init();
   if(curl) {
       curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
       curl_easy_setopt(curl, CURLOPT_URL, "https://api.labsmobile.com/json/balance");
       curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
       curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
       curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_BASIC);
       curl_easy_setopt(curl, CURLOPT_USERNAME, "myUsername");
       curl_easy_setopt(curl, CURLOPT_PASSWORD, "myToken");
       struct curl_slist *headers = NULL;
       curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
       const char *data = "";
       curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
       res = curl_easy_perform(curl);
       if(res != CURLE_OK)
           fprintf(stderr, "curl_easy_perform() failed: %s\n",
                     curl_easy_strerror(res));
       curl_easy_cleanup(curl);
   }
   return 0;
}
                

Result
{"code":0,"credits":"10"}

  

See the complete documentation at:


Receive status of sent messages

This example script receives a call from the LabsMobile platform when a sent SMS message changes status. To implement this solution, it is essential to configure the appropriate parameters, the confirmation URL in the ackurl parameter in the request or the default URL in the API Settings of your account.

Therefore, it is necessary to publish a script like this in your system so that our platform calls the URL when a status change occurs in any of the sent messages.

The example code obtains the URL parameters using the global $_GET variable and then assigns these parameters to local variables.

Script for receiving sent message status (C)
#define PORT 3000
#define BUFFER_SIZE 1024

void process_params(char *url) {
   char *param;
   char *saveptr;
   param = strtok_r(url, "&", &saveptr);
   while (param != NULL) {
       printf("Parameter: %s\n", param);
       param = strtok_r(NULL, "&", &saveptr);
   }
}

int main() {
   int server_fd, new_socket, valread;
   struct sockaddr_in address;
   int addrlen = sizeof(address);
   char buffer[BUFFER_SIZE] = {0};
  
   if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
       perror("socket failed");
       exit(EXIT_FAILURE);
   }
  
   address.sin_family = AF_INET;
   address.sin_addr.s_addr = INADDR_ANY;
   address.sin_port = htons(PORT);
  
   if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
       perror("bind failed");
       exit(EXIT_FAILURE);
   }
  
   if (listen(server_fd, 3) < 0) {
       perror("listen");
       exit(EXIT_FAILURE);
   }
  
   printf("Server listening on port %d\n", PORT);
  
   while (true) {
       if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) {
           perror("accept");
           exit(EXIT_FAILURE);
       }
      
       valread = read(new_socket, buffer, BUFFER_SIZE);

       char *url_start = strstr(buffer, "GET /");
       if (url_start != NULL) {
           url_start += 5;
           char *url_end = strstr(url_start, "HTTP/1.1");
           if (url_end != NULL) {
               *url_end = 'API';
               process_params(url_start);
           }
       }
      
       const char *response = "HTTP/1.1 200 OK\nContent-Type: text/plain\n\n";
       send(new_socket, response, strlen(response), 0);
       close(new_socket);
   }
  
   return 0;
}               
                

See the complete documentation at:


Receiving SMS messages

Once you have contracted a virtual number, you will be able to receive messages via HTTP/GET on a specific URL via a JSON call to a script in your system. Each message received will invoke the URL, transmitting all its data in variables in $_GET format.

To activate this functionality you must inform the URL for receiving messages in the Settings of your account.

It is necessary to configure an endpoint in your system so that the LabsMobile platform calls this script when an SMS is received on any of the contracted virtual numbers.

The example code obtains the URL parameters using the global variable

#define PORT 3000
#define BUFFER_SIZE 1024

void process_params(char *url) {
   char *param;
   char *saveptr;
   param = strtok_r(url, "&", &saveptr);
   while (param != NULL) {
       printf("Parameter: %s\n", param);
       param = strtok_r(NULL, "&", &saveptr);
   }
}

int main() {
   int server_fd, new_socket, valread;
   struct sockaddr_in address;
   int addrlen = sizeof(address);
   char buffer[BUFFER_SIZE] = {0};
  
   if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
       perror("socket failed");
       exit(EXIT_FAILURE);
   }
  
   address.sin_family = AF_INET;
   address.sin_addr.s_addr = INADDR_ANY;
   address.sin_port = htons(PORT);
  
   if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0 ) {
       perror("bind failed");
       exit(EXIT_FAILURE);
   }
  
   if (listen(server_fd, 3) < 0) {
       perror("listen");
       exit(EXIT_FAILURE);
   }
  
   printf("Server listening on port %d\n", PORT);
  
   while (true) {
       if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0 ) {
           perror("accept");
           exit(EXIT_FAILURE);
       }
      
       valread = read(new_socket, buffer, BUFFER_SIZE);

       char *url_start = strstr(buffer, "GET /");
       if (url_start != NULL) {
           url_start += 5;
           char *url_end = strstr(url_start, "HTTP/1.1");
           if (url_end != NULL) {
               *url_end = '
';
               process_params(url_start);
           }
       }
      
       const char *response = "HTTP/1.1 200 OK\nContent-Type: text/plain\n\n";
       send(new_socket, response, strlen(response), 0);
       close(new_socket);
   }
  
   return 0;
}

                
and then assigns these parameters to local variables.

Script for receiving incoming messages (C)

See the complete documentation at:


@@PRESERVEDTAG@@@@PRESERVEDTAG@@