Sample code for 30+ languages & platforms
Objective-C Requires Chilkat v11.0.0+

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

Clickatell API reference

Chilkat Objective-C Downloads

Objective-C
#import <CkoHttp.h>
#import <CkoJsonObject.h>
#import <NSString.h>
#import <CkoHttpResponse.h>

BOOL success = NO;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
CkoHttp *http = [[CkoHttp alloc] init];
http.RequireSslCertVerify = YES;
//  Supply the key for this One API integration from application configuration.
[http SetRequestHeader: @"Authorization" value: @"ONE_API_KEY"];
[http SetRequestHeader: @"Accept" value: @"application/json"];

CkoJsonObject *body = [[CkoJsonObject alloc] init];
[body UpdateString: @"messages[0].channel" value: @"sms"];
[body UpdateString: @"messages[0].to" value: @"PHONE_NUMBER"];
[body UpdateString: @"messages[0].content" value: @"Test message: your order is ready."];
//  For a two-way sender, supply the number provisioned on the integration.
NSString *sender = @"";
if (sender != @"") {
    [body UpdateString: @"messages[0].from" value: sender];
}

CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];
success = [http HttpJson: @"POST" url: @"https://platform.clickatell.com/v1/message" json: body contentType: @"application/json" response: resp];
if ([resp.StatusCode intValue] == 0) {
    NSLog(@"%@",@"The send outcome is unknown. Check the message report before resending.");
    return;
}

CkoJsonObject *reply = [[CkoJsonObject alloc] init];
BOOL parsed = [reply Load: resp.BodyStr];
if ((success != YES) || (([resp.StatusCode intValue] != 202) && ([resp.StatusCode intValue] != 207))) {
    NSLog(@"%@%d",@"Clickatell returned HTTP ",[resp.StatusCode intValue]);
    if (parsed == YES) {
        if ([reply HasMember: @"error.description"] == YES) {
            NSLog(@"%@",[reply StringOf: @"error.description"]);
        }

    }

    return;
}

if (parsed != YES) {
    NSLog(@"%@",@"Expected a JSON response.");
    return;
}

//  A request-level error applies to the submission as a whole.
if ([reply HasMember: @"error.code"] == YES) {
    if ([reply IsNullOf: @"error.code"] != YES) {
        NSLog(@"%@%@%@%@",@"Submission error ",[reply StringOf: @"error.code"],@"  ",[reply StringOf: @"error.description"]);
        return;
    }

}

int count = [[reply SizeOfArray: @"messages"] intValue];
if (count <= 0) {
    NSLog(@"%@",@"No recipient results returned. Check the message report before resending.");
    return;
}

int i = 0;
while ((i < count)) {
    reply.I = [NSNumber numberWithInt: i];
    int resultNumber = i + 1;
    //  JsonTypeOf returns 5 for a JSON boolean.
    if ([[reply JsonTypeOf: @"messages[i].accepted"] intValue] != 5) {
        NSLog(@"%@%d%@",@"Result ",resultNumber,@" has no acceptance flag.");
    }
    else {
        if ([reply BoolOf: @"messages[i].accepted"] == YES) {
            NSString *messageId = [reply StringOf: @"messages[i].apiMessageId"];
            if (([[reply JsonTypeOf: @"messages[i].apiMessageId"] intValue] != 1) || (messageId == @"")) {
                NSLog(@"%@%d%@",@"Result ",resultNumber,@" was accepted without a tracking ID.");
            }
            else {
                NSLog(@"%@%d%@%@",@"Result ",resultNumber,@" accepted. Message ID: ",messageId);
            }

        }
        else {
            NSLog(@"%@%d%@%@%@%@",@"Result ",resultNumber,@" rejected: ",[reply StringOf: @"messages[i].error.code"],@"  ",[reply StringOf: @"messages[i].error.description"]);
        }

    }

    i = i + 1;
}