Clickatell One API: Send an SMS and get its message ID
See more Clickatell Examples
Send one text using an SMS-enabled One API integration. Supply an international phone number starting with the country code, using digits only. For a US two-way integration, set from to its provisioned two-way number. With a test integration, use a registered test phone.
HTTP 202 means the submission was processed. Inspect accepted for each message. Save the returned apiMessageId with the application record and use it to look up delivery status. When a multi-message submission returns HTTP 207, check each result before retrying any rejected message.
Already using the SMS-only REST integration? Its documented request is POST /messages with the integration key in Authorization and a body such as {"content":"Test message","to":["PHONE_NUMBER"]}. One API has a different request body and a separate integration key. SMS API reference
Chilkat Lazarus Pascal Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.Http,
Chilkat.HttpResponse,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
http: THttp;
body: TJsonObject;
sender: string;
resp: THttpResponse;
reply: TJsonObject;
parsed: Boolean;
count: Integer;
i: Integer;
resultNumber: Integer;
messageId: string;
begin
success := False;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
http := THttp.Create;
http.RequireSslCertVerify := True;
// Supply the key for this One API integration from application configuration.
http.SetRequestHeader('Authorization','ONE_API_KEY');
http.SetRequestHeader('Accept','application/json');
body := TJsonObject.Create;
body.UpdateString('messages[0].channel','sms');
body.UpdateString('messages[0].to','PHONE_NUMBER');
body.UpdateString('messages[0].content','Test message: your order is ready.');
// For a two-way sender, supply the number provisioned on the integration.
sender := '';
if (sender <> '') then
begin
body.UpdateString('messages[0].from',sender);
end;
resp := THttpResponse.Create;
success := http.HttpJson('POST','https://platform.clickatell.com/v1/message',body,'application/json',resp);
if (resp.StatusCode = 0) then
begin
WriteLn('The send outcome is unknown. Check the message report before resending.');
Exit;
end;
reply := TJsonObject.Create;
parsed := reply.Load(resp.BodyStr);
if ((success <> True) or ((resp.StatusCode <> 202) and (resp.StatusCode <> 207))) then
begin
WriteLn('Clickatell returned HTTP ' + resp.StatusCode);
if (parsed = True) then
begin
if (reply.HasMember('error.description') = True) then
begin
WriteLn(reply.StringOf('error.description'));
end;
end;
Exit;
end;
if (parsed <> True) then
begin
WriteLn('Expected a JSON response.');
Exit;
end;
// A request-level error applies to the submission as a whole.
if (reply.HasMember('error.code') = True) then
begin
if (reply.IsNullOf('error.code') <> True) then
begin
WriteLn('Submission error ' + reply.StringOf('error.code') + ' ' + reply.StringOf('error.description'));
Exit;
end;
end;
count := reply.SizeOfArray('messages');
if (count <= 0) then
begin
WriteLn('No recipient results returned. Check the message report before resending.');
Exit;
end;
i := 0;
while (i < count) do
begin
reply.I := i;
resultNumber := i + 1;
// JsonTypeOf returns 5 for a JSON boolean.
if (reply.JsonTypeOf('messages[i].accepted') <> 5) then
begin
WriteLn('Result ' + resultNumber + ' has no acceptance flag.');
end
else
begin
if (reply.BoolOf('messages[i].accepted') = True) then
begin
messageId := reply.StringOf('messages[i].apiMessageId');
if ((reply.JsonTypeOf('messages[i].apiMessageId') <> 1) or (messageId = '')) then
begin
WriteLn('Result ' + resultNumber + ' was accepted without a tracking ID.');
end
else
begin
WriteLn('Result ' + resultNumber + ' accepted. Message ID: ' + messageId);
end;
end
else
begin
WriteLn('Result ' + resultNumber + ' rejected: ' + reply.StringOf('messages[i].error.code') + ' ' + reply.StringOf('messages[i].error.description'));
end;
end;
i := i + 1;
end;
http.Free;
body.Free;
resp.Free;
reply.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.