Sample code for 30+ languages & platforms
Delphi DLL Requires Chilkat v11.0.0+

Clickatell One API: Send personalized SMS reminders in a batch

See more Clickatell Examples

Send two appointments in one request with a different text and application ID for each. Use two distinct recipient numbers in international digits. For a US two-way integration, add its provisioned from number to each message.

Read each recipient result even when HTTP 207 reports a mixed outcome. Save the accepted provider ID against the appointment. A missing result calls for checking the message report before retrying. A client message ID helps correlate records and does not guarantee deduplication.

Clickatell API reference

Chilkat Delphi DLL Downloads

Delphi DLL
var
http: HCkHttp;
firstPhone: PWideChar;
secondPhone: PWideChar;
body: HCkJsonObject;
resp: HCkHttpResponse;
sent: Boolean;
reply: HCkJsonObject;
parsed: Boolean;
results: HCkJsonObject;
i: Integer;
appointment: PWideChar;
phone: PWideChar;
result: HCkJsonObject;
found: Boolean;
saved: Boolean;

begin
//  Assumes Chilkat is unlocked. See Global Unlock Sample.
http := CkHttp_Create();
CkHttp_putRequireSslCertVerify(http,True);
//  Load the key for this One API integration from your application configuration.
CkHttp_SetRequestHeader(http,'Authorization','ONE_API_KEY');
CkHttp_SetRequestHeader(http,'Accept','application/json');

firstPhone := 'FIRST_PHONE_NUMBER';
secondPhone := 'SECOND_PHONE_NUMBER';
body := CkJsonObject_Create();
CkJsonObject_UpdateString(body,'messages[0].channel','sms');
CkJsonObject_UpdateString(body,'messages[0].to',firstPhone);
CkJsonObject_UpdateString(body,'messages[0].content','Your appointment with Lee is at 10:00.');
CkJsonObject_UpdateString(body,'messages[0].clientMessageId','appt-1042');
CkJsonObject_UpdateString(body,'messages[1].channel','sms');
CkJsonObject_UpdateString(body,'messages[1].to',secondPhone);
CkJsonObject_UpdateString(body,'messages[1].content','Your appointment with Sam is at 14:30.');
CkJsonObject_UpdateString(body,'messages[1].clientMessageId','appt-1043');

resp := CkHttpResponse_Create();
sent := CkHttp_HttpJson(http,'POST','https://platform.clickatell.com/v1/message',body,'application/json',resp);
if (CkHttpResponse_getStatusCode(resp) = 0) then
  begin
    Memo1.Lines.Add('No HTTP response. Check the message report before resending.');
    Exit;
  end;
reply := CkJsonObject_Create();
parsed := CkJsonObject_Load(reply,CkHttpResponse__bodyStr(resp));
if ((sent <> True) or ((CkHttpResponse_getStatusCode(resp) <> 202) and (CkHttpResponse_getStatusCode(resp) <> 207))) then
  begin
    Memo1.Lines.Add('Send returned HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)));
    if (CkJsonObject_HasMember(reply,'error.description') = True) then
      begin
        Memo1.Lines.Add(CkJsonObject__stringOf(reply,'error.description'));
      end;
    Exit;
  end;
if (CkJsonObject_HasMember(reply,'error.code') = True) then
  begin
    if (CkJsonObject_IsNullOf(reply,'error.code') <> True) then
      begin
        Memo1.Lines.Add('Submission error ' + CkJsonObject__stringOf(reply,'error.code') + '  ' + CkJsonObject__stringOf(reply,'error.description'));
        Exit;
      end;
  end;

//  Look up each submitted appointment in the reply using its unique recipient.
//  This works even if the result order changes or clientMessageId is absent.
results := CkJsonObject_Create();
i := 0;
while (i < 2) do
  begin
    CkJsonObject_putI(body,i);
    appointment := CkJsonObject__stringOf(body,'messages[i].clientMessageId');
    phone := CkJsonObject__stringOf(body,'messages[i].to');
    result := CkJsonObject_Create();
    found := CkJsonObject_FindRecord2(reply,'messages','to',phone,True,result);
    CkJsonObject_putI(results,i);
    CkJsonObject_UpdateString(results,'appointments[i].clientMessageId',appointment);
    if ((found <> True) or (CkJsonObject_JsonTypeOf(result,'accepted') <> 5)) then
      begin
        CkJsonObject_UpdateString(results,'appointments[i].outcome','unknown');
        Memo1.Lines.Add(appointment + ' has no confirmed result.');
      end
    else
      begin
        if (CkJsonObject_BoolOf(result,'accepted') = True) then
          begin
            CkJsonObject_UpdateString(results,'appointments[i].outcome','accepted');
            if (CkJsonObject_JsonTypeOf(result,'apiMessageId') = 1) then
              begin
                CkJsonObject_UpdateString(results,'appointments[i].apiMessageId',CkJsonObject__stringOf(result,'apiMessageId'));
              end;
            Memo1.Lines.Add(appointment + ' accepted.');
          end
        else
          begin
            CkJsonObject_UpdateString(results,'appointments[i].outcome','rejected');
            CkJsonObject_UpdateString(results,'appointments[i].reason',CkJsonObject__stringOf(result,'error.description'));
            Memo1.Lines.Add(appointment + ' rejected: ' + CkJsonObject__stringOf(result,'error.description'));
          end;
      end;
    i := i + 1;
  end;

//  This demo writes a snapshot. In an application, update the two appointment records.
saved := CkJsonObject_WriteFile(results,'appointment-send-results.json');
if (saved <> True) then
  begin
    Memo1.Lines.Add('Could not save the results. Reconcile the submitted messages before retrying.');
    Exit;
  end;
Memo1.Lines.Add('Saved appointment-send-results.json');

CkHttp_Dispose(http);
CkJsonObject_Dispose(body);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(reply);
CkJsonObject_Dispose(results);
    CkJsonObject_Dispose(result);