ES

Delphi code samples for integration with the LabsMobile SMS API

The following are the examples of programming code in Delphi 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 Delphi 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 (Delphi)
uses
  IdHTTP, IdURI, SysUtils, Classes;

procedure SendSMS;
var
  http: TIdHTTP;
  url, data: string;
  requestBody: TStringStream;
begin
  http := TIdHTTP.Create(nil);
  try
    url := 'https://api.labsmobile.com/json/send';

    data := '{
      "message":"Your verification code is 123", 
      "tpoa":"Sender",
      "recipient":[
        {
          "msisdn":"12015550123"
        }
      ]
    }';

    requestBody := TStringStream.Create(data, TEncoding.UTF8);
    try
      http.Request.BasicAuthentication := True;
      http.Request.Username := 'myUsername';
      http.Request.Password := 'myToken';
      http.Request.ContentType := 'application/json';

      Memo1.Lines.Text := http.Post(url, requestBody);
    finally
      requestBody.Free;
    end;
  finally
    http.Free;
  end;
end;
              

Bulk sending to multiple recipients with http/POST (Delphi)
uses
  IdHTTP, IdURI, SysUtils, Classes;

procedure SendSMS;
var
  http: TIdHTTP;
  url, data: string;
  requestBody: TStringStream;
begin
  http := TIdHTTP.Create(nil);
  try
    url := 'https://api.labsmobile.com/json/send';

    data := '{
      "message":"Do not miss our Sale! Use code XXXX123 for 20%off.", 
      "tpoa":"Sender",
      "recipient":[
        {
          "msisdn":"12015550123"
        },
        {
          "msisdn":"12015550124"
        },
        {
          "msisdn":"12015550125"
        }
      ]
    }';

    requestBody := TStringStream.Create(data, TEncoding.UTF8);
    try
      http.Request.BasicAuthentication := True;
      http.Request.Username := myUsername;
      http.Request.Password := myToken;
      http.Request.ContentType := 'application/json';

      Memo1.Lines.Text := http.Post(url, requestBody);
    finally
      requestBody.Free;
    end;
  finally
    http.Free;
  end;
end;

begin
  SendSMS;
end.
                

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 Delphi language to send SMS messages using the SMS API http/GET.

As you can 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 TIdURI.URLEncode() 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 (Delphi)
uses
  IdHTTP, IdURI;

function getContent(url: String): String;
var
  http: TIdHTTP;
begin
  http := TIdHTTP.Create(nil);
  try
    Result := http.Get(url);
  finally
    http.Free;
  end;
end;

var
  myUsername, myToken, msisdn, sender, messageContent: string;
begin
  myUsername := 'tuUsuario';
  myToken := 'tuToken';
  msisdn := '12015550123';
  sender := 'Sender';
  messageContent := 'Your verification code is 123';

  Memo.Lines.Add(getContent(
    Format('http://api.labsmobile.com/get/send.php?username=%s&password=%s&msisdn=%s&sender=%s&message=%s',
    [TIdURI.URLEncode(myUsername), TIdURI.URLEncode(myToken), TIdURI.URLEncode(msisdn), TIdURI.URLEncode(sender), TIdURI.URLEncode(messageContent)])));
end;
                

Bulk sending to multiple recipients with http/GET (Delphi)
uses
  IdHTTP, IdURI;

function getContent(url: String): String;
var
  http: TIdHTTP;
begin
  http := TIdHTTP.Create(nil);
  try
    Result := http.Get(url);
  finally
    http.Free;
  end;
end;

var
  myUsername, myToken, msisdn, sender, messageContent: string;
begin
  myUsername := 'tuUsuario';
  myToken := 'tuToken';
  msisdn := msisdns := '[12015550123,12015550124,12015550125]';
  sender := 'Sender';
  messageContent := 'Do not miss our Sale! Use code XXXX123 for 20%off.';

  Memo.Lines.Add(getContent(
    Format('http://api.labsmobile.com/get/send.php?username=%s&password=%s&msisdn=%s&sender=%s&message=%s',
    [TIdURI.URLEncode(myUsername), TIdURI.URLEncode(myToken), TIdURI.URLEncode(msisdn), TIdURI.URLEncode(sender), TIdURI.URLEncode(messageContent)])));
end;
                

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 Delphi 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 (Delphi)
uses
  IdHTTP, IdURI, SysUtils, Classes;

procedure Balance;
var
  http: TIdHTTP;
  url, data: string;
  requestBody: TStringStream;
begin
  http := TIdHTTP.Create(nil);
  try
    url := 'https://api.labsmobile.com/json/balance';

    data := '';

    requestBody := TStringStream.Create(data, TEncoding.UTF8);
    try
      http.Request.BasicAuthentication := True;
      http.Request.Username := 'myUsername';
      http.Request.Password := 'myToken';
      http.Request.ContentType := 'application/json';

      Memo1.Lines.Text := http.Get(url, requestBody);
    finally
      requestBody.Free;
    end;
  finally
    http.Free;
  end;
end;

begin
  Balance;
end.
                

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 status of sent messages (Delphi)
program ExampleCode;

{$APPTYPE CONSOLE}

uses
  SysUtils, SynCommons, mORMot;

const
  PORT = 3000;

var
  server: TSQLHttpServer;

procedure HandleRequest(const ctxt: TSQLRestServerURIContext);
var
  acklevel, credits, msisdn, status, subid, timestamp: RawUTF8;
begin
  ctxt.Success;
  acklevel := ctxt.Parameters['acklevel'];
  credits := ctxt.Parameters['credits'];
  msisdn := ctxt.Parameters['msisdn'];
  status := ctxt.Parameters['status'];
  subid := ctxt.Parameters['subid'];
  timestamp := ctxt.Parameters['timestamp'];

  WriteLn('Variable acklevel : ', acklevel);
  WriteLn('Variable credits : ', credits);
  WriteLn('Variable msisdn : ', msisdn);
  WriteLn('Variable status : ', status);
  WriteLn('Variable subid : ', subid);
  WriteLn('Variable timestamp : ', timestamp);
end;

begin
  server := TSQLHttpServer.Create(IntToStr(PORT), []);
  try
    server.OnCustomURI := HandleRequest;
    server.AccessControlAllowOrigin := '*';
    server.AccessControlAllowHeaders := '*';
    server.AccessControlAllowMethods := '*'; 

    WriteLn('The server running on port ', PORT);

    server.Start(PORT);
    WriteLn('Press Enter to stop the server...');
    ReadLn;
  finally
    server.Free;
  end;
end.
                

See the complete documentation at:


Receiving SMS messages

Once you have contracted a virtual number, you will be able to receive messages via API on a specific URL via a HTTP/GET call to a script in your system. Each message received will invoke the URL, transmitting all its data in variables in JSON 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 $_GET and then assigns these parameters to local variables.

Script for receiving incoming messages (Delphi)
program ExampleCode;

{$APPTYPE CONSOLE}

uses
  SysUtils, SynCommons, mORMot;

const
  PORT = 3000;

var
  server: TSQLHttpServer;

procedure HandleRequest(const ctxt: TSQLRestServerURIContext);
var
  inbound_number, service_number, msisdn, message, timestamp: RawUTF8;
begin
  ctxt.Success; // Indicar que la solicitud fue exitosa

  // Obtener los parámetros de consulta
  inbound_number := ctxt.Parameters['inbound_number'];
  service_number := ctxt.Parameters['service_number'];
  msisdn := ctxt.Parameters['msisdn'];
  message := ctxt.Parameters['message'];
  timestamp := ctxt.Parameters['timestamp'];

  // Mostrar los parámetros en la consola
  WriteLn('Variable inbound_number : ', inbound_number);
  WriteLn('Variable service_number : ', service_number);
  WriteLn('Variable msisdn : ', msisdn);
  WriteLn('Variable message : ', message);
  WriteLn('Variable timestamp : ', timestamp);
end;

begin
  server := TSQLHttpServer.Create(IntToStr(PORT), []);
  try
    // Configurar la ruta para manejar solicitudes GET a "/"
    server.OnCustomURI := HandleRequest;
    server.AccessControlAllowOrigin := '*'; // Permitir solicitudes de cualquier origen
    server.AccessControlAllowHeaders := '*'; // Permitir cualquier encabezado en la solicitud
    server.AccessControlAllowMethods := '*'; // Permitir cualquier método de solicitud (GET, POST, etc.)

    WriteLn('The server running on port ', PORT);

    // Iniciar el servidor
    server.Start(PORT);
    WriteLn('Press Enter to stop the server...');
    ReadLn;

  finally
    server.Free;
  end;
end.
                

See the complete documentation at: