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 Delphi DLL Downloads
var
success: Boolean;
http: HCkHttp;
body: HCkJsonObject;
sender: PWideChar;
resp: HCkHttpResponse;
reply: HCkJsonObject;
parsed: Boolean;
count: Integer;
i: Integer;
resultNumber: Integer;
messageId: PWideChar;
begin
success := False;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
http := CkHttp_Create();
CkHttp_putRequireSslCertVerify(http,True);
// Supply the key for this One API integration from application configuration.
CkHttp_SetRequestHeader(http,'Authorization','ONE_API_KEY');
CkHttp_SetRequestHeader(http,'Accept','application/json');
body := CkJsonObject_Create();
CkJsonObject_UpdateString(body,'messages[0].channel','sms');
CkJsonObject_UpdateString(body,'messages[0].to','PHONE_NUMBER');
CkJsonObject_UpdateString(body,'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
CkJsonObject_UpdateString(body,'messages[0].from',sender);
end;
resp := CkHttpResponse_Create();
success := CkHttp_HttpJson(http,'POST','https://platform.clickatell.com/v1/message',body,'application/json',resp);
if (CkHttpResponse_getStatusCode(resp) = 0) then
begin
Memo1.Lines.Add('The send outcome is unknown. Check the message report before resending.');
Exit;
end;
reply := CkJsonObject_Create();
parsed := CkJsonObject_Load(reply,CkHttpResponse__bodyStr(resp));
if ((success <> True) or ((CkHttpResponse_getStatusCode(resp) <> 202) and (CkHttpResponse_getStatusCode(resp) <> 207))) then
begin
Memo1.Lines.Add('Clickatell returned HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)));
if (parsed = True) then
begin
if (CkJsonObject_HasMember(reply,'error.description') = True) then
begin
Memo1.Lines.Add(CkJsonObject__stringOf(reply,'error.description'));
end;
end;
Exit;
end;
if (parsed <> True) then
begin
Memo1.Lines.Add('Expected a JSON response.');
Exit;
end;
// A request-level error applies to the submission as a whole.
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;
count := CkJsonObject_SizeOfArray(reply,'messages');
if (count <= 0) then
begin
Memo1.Lines.Add('No recipient results returned. Check the message report before resending.');
Exit;
end;
i := 0;
while (i < count) do
begin
CkJsonObject_putI(reply,i);
resultNumber := i + 1;
// JsonTypeOf returns 5 for a JSON boolean.
if (CkJsonObject_JsonTypeOf(reply,'messages[i].accepted') <> 5) then
begin
Memo1.Lines.Add('Result ' + IntToStr(resultNumber) + ' has no acceptance flag.');
end
else
begin
if (CkJsonObject_BoolOf(reply,'messages[i].accepted') = True) then
begin
messageId := CkJsonObject__stringOf(reply,'messages[i].apiMessageId');
if ((CkJsonObject_JsonTypeOf(reply,'messages[i].apiMessageId') <> 1) or (messageId = '')) then
begin
Memo1.Lines.Add('Result ' + IntToStr(resultNumber) + ' was accepted without a tracking ID.');
end
else
begin
Memo1.Lines.Add('Result ' + IntToStr(resultNumber) + ' accepted. Message ID: ' + messageId);
end;
end
else
begin
Memo1.Lines.Add('Result ' + IntToStr(resultNumber) + ' rejected: ' + CkJsonObject__stringOf(reply,'messages[i].error.code') + ' ' + CkJsonObject__stringOf(reply,'messages[i].error.description'));
end;
end;
i := i + 1;
end;
CkHttp_Dispose(http);
CkJsonObject_Dispose(body);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(reply);