Skip to content
Codelia SMSDocs
Guides: Sending messages

Sending messages

POST /organizations/{accountId}/messages sends one text to up to 500 numbers. Each number gets its own message, with its own price and status. To send to a list, fill in each person's name, or send later, use a campaign instead.

Send a message

curl -X POST "https://api.sms.codelia.studio/v1/organizations/$CODELIA_ACCOUNT_ID/messages" \
  -H "Authorization: Bearer $CODELIA_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: order-1042-ready" \
  -d '{
  "to": [
    "+442079460123"
  ],
  "from": "InfoSMS",
  "body": "Your order 1042 is ready to collect."
}'

The body takes:

FieldWhat to send
to1 to 500 numbers in international format, like +442079460123. The same number, written the same way, is sent once.
bodyThe text, up to 1,600 characters. Send body or bodies, never both.
bodiesOne text per language, instead of body. See Send in each person's language.
fromThe sender name. Leave it out to use your default.
typemarketing (the default) or transactional, once approved. See Transactional messages.

The API key needs messages:send. The answer is 200 with one entry per number: its id, status, segments (the parts it was counted as), priceMicros and errorCode. Keep the id to look the message up later.

A number that opted out of your marketing messages is in the answer too, with status: "failed", errorCode: "opted_out" and priceMicros: 0. It isn't sent or charged. See Opt-outs.

A number we can't send to is in the answer the same way, with errorCode: "unsupported_number", for either type of message. So is a number in a country your organization can't send to yet, with errorCode: "country_not_allowed". Neither is sent or charged, and the rest of the numbers are sent. If no other number is left to send to, the request is refused with a 400 instead: all_recipients_not_allowed if one of the numbers is in such a country, otherwise all_recipients_unsupported. To send to a country your organization can't send to yet, email hello@codelia.studio.

Add an Idempotency-Key header every time you send, so a retry after a timeout can't send twice. See Retrying safely.

Send in each person's language

Send bodies instead of body, keyed by language code, and give a number its language with { "to": …, "language": … }:

curl -X POST "https://api.sms.codelia.studio/v1/organizations/$CODELIA_ACCOUNT_ID/messages" \
  -H "Authorization: Bearer $CODELIA_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: order-1042-ready-languages" \
  -d '{
  "to": [
    {
      "to": "+442079460123",
      "language": "el"
    },
    "+442079460124"
  ],
  "from": "InfoSMS",
  "bodies": {
    "en": "Your order 1042 is ready to collect.",
    "el": "Η παραγγελία σας 1042 είναι έτοιμη για παραλαβή."
  }
}'

Each number gets the text for its language, picked in this order:

  1. the language on its entry in to;
  2. otherwise, the preferred language of the contact with that number;
  3. otherwise, your organization's default language.

A number whose language has no text in bodies gets the default language's text, so bodies must include your default language, or the request is refused with default_variant_required. We don't detect a language from the text: each number gets the text its language points to, and it's priced on that text.

Sender names

The sender name is who the message appears to come from. When you send, it's picked in this order:

  1. the from in the request;
  2. otherwise, your organization's default sender name;
  3. otherwise nothing, and the request is refused with 400 and sender_id_required.

The name must be approved for your organization, or be InfoSMS, a shared sender name every organization can use in every country. A sender name can be limited to some countries: if one number in to is outside them, the whole request is refused with sender_country_not_allowed. A campaign isn't refused for it: only that person's message fails.

List your sender names, with each one's status and which is the default (isDefault), with sender-ids:read:

curl "https://api.sms.codelia.studio/v1/organizations/$CODELIA_ACCOUNT_ID/sender-ids" \
  -H "Authorization: Bearer $CODELIA_API_KEY"

To ask for a new one, send its value to POST /organizations/{accountId}/sender-ids with sender-ids:write. It starts as pending, and we check it before you can send with it. A name with letters in it can be up to 11 characters, with no spaces. A number can be up to 15 digits. You can add a description of what you'll send from it, up to 500 characters, and countries, the two-letter codes of the countries you'll use it in. Asking again for a value you already have returns that sender name as it is. Choose your default on the Sender names screen in the portal.

How messages are counted

Messages are counted and priced in parts:

  • A message of up to 160 characters is one part. A longer one is split into parts of 153 characters.
  • One character outside the standard SMS alphabet, like an emoji or an accented letter such as á, makes the whole message use 70 characters for one part, and 67 per part once it's split.
  • € [ ] { } | ^ ~ \ each count as two characters.
  • A marketing message ends with the unsubscribe line, which adds 41 characters, its line break included. It's counted and priced with your text. A transactional message has no unsubscribe line.
Text sent as a marketing messagePartsWhy
Your order 1042 is ready to collect.136 characters and the 41 of the unsubscribe line: 77, within 160.
Your order 1042 is ready to collect 🎉2The emoji counts as two and moves the message to 70 a part: 38 and 41 is 79.
A 136-character offer, with no emoji2136 and 41 is 177, over 160, so it's split into parts of 153.

The answer's segments is the number of parts each message was counted as.

Price it first

POST /organizations/{accountId}/messages/quote takes the same body as POST …/messages, never sends or charges, and accepts up to 10,000 numbers. The API key needs messages:read:

curl -X POST "https://api.sms.codelia.studio/v1/organizations/$CODELIA_ACCOUNT_ID/messages/quote" \
  -H "Authorization: Bearer $CODELIA_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "to": [
    "+442079460123"
  ],
  "from": "InfoSMS",
  "body": "Your order 1042 is ready to collect."
}'

The answer gives totalMicros and currency, pricedCount, unpriceableCount and notAllowedCount, senderApproved, and a line for each language with its parts (segments.min and segments.max). Each number costs its country's price per part, times its parts. Billing explains list prices and agreed rates.

The total is an estimated cost. When you send, the price is worked out again, and that's what you're charged. Numbers in a country your organization can't send to yet are left out of the price and counted in notAllowedCount. Opt-outs and numbers we can't send to are still priced, so sending can cost less than the estimated cost. See Billing.

After you send

A message you send starts as sending: it's with the network. A campaign's message is queued until it's handed over. When a delivery report comes back it moves to delivered, undelivered, failed or rejected. There's no set time for a report to arrive, and some never do.

outcome says the same in the portal's words: pending (a campaign's message not yet handed over), sent, delivered, not_delivered, not_sent or opted_out. not_sent means we didn't send it and didn't charge for it: its errorCode is unsupported_number or country_not_allowed.

A message that didn't arrive has an errorCode:

  • Decided by the network: invalid_number, unreachable, blocked, expired, network_error, message_rejected.
  • Decided by us, before or instead of handing it over: opted_out, unsupported_number, country_not_allowed, provider_unavailable, invalid_recipient, sender_country_not_allowed, no_rate_for_country, country_disabled, currency_mismatch, rendered_text_too_long, campaign_cancelled.
  • A campaign's template couldn't be filled in: missing_variable, unknown_variable, invalid_variable_name, value_too_long, rendered_text_empty.

New codes can be added. Treat one you don't know as a failure.

Read one message with GET /organizations/{accountId}/messages/{messageId}, or list them, newest first, with messages:read:

curl "https://api.sms.codelia.studio/v1/organizations/$CODELIA_ACCOUNT_ID/messages?limit=20" \
  -H "Authorization: Bearer $CODELIA_API_KEY"

The list takes status (repeat it for several), outcome, type, createdFrom (inclusive) and createdTo (exclusive). It answers with up to limit messages in entries (up to 200, and 50 if you leave it out), and a nextCursor to pass back as cursor for the next page. See Paging through results. To be told when a message is delivered or fails instead of asking, use Webhooks.

Send again

Send the same text to the same number again, as a new message of the same type, priced and charged at today's price. The first message doesn't change. The API key needs messages:send:

curl -X POST "https://api.sms.codelia.studio/v1/organizations/$CODELIA_ACCOUNT_ID/messages/$MESSAGE_ID/resend" \
  -H "Authorization: Bearer $CODELIA_API_KEY" \
  -H "Idempotency-Key: order-1042-ready-again"

Use a different key for each message you send again. A key is tied to the message's path, so reusing one for a second message is refused with 422 and idempotency_key_reuse.

When it's refused

When a request is refused, nothing is sent or charged, except where the table says otherwise.

StatuserrorWhat happenedWhat to do
400sender_id_requiredNo from, and your organization has no default sender name.Send from, or choose a default sender name in the portal.
400unapproved_sender_idThe sender name isn't approved for your organization.Use an approved sender name, or InfoSMS.
400sender_country_not_allowedThe sender name can't be used for one of the numbers' countries.Use another sender name for that country.
400default_variant_requiredbodies has no text for your default language.Add it to bodies.
400invalid_recipientA number couldn't be matched to a country.Use international format, like +442079460123.
400no_rate_for_countryWe don't send to one of the numbers' countries.Remove those numbers.
400all_recipients_opted_outEvery number opted out of your marketing messages.Don't retry them.
400all_recipients_unsupportedWe can't send to any of the numbers. Anyone else in the request opted out. Nothing was sent or charged.Check the numbers are right. If they are, email hello@codelia.studio.
400all_recipients_not_allowedYour organization can't send to these countries yet, and no one else in the request is left to send to. Nothing was sent or charged.Email hello@codelia.studio to send to them.
402insufficient_creditsYour balance can't cover the messages.Top up, then send again with a new Idempotency-Key.
403country_disabledSending to one of the numbers' countries isn't available right now.Remove those numbers, or try again later.
502provider_unavailableThe messages couldn't be handed to the network. The description says whether you were refunded.Check your balance and the message's status before you send again.

Every code, including the ones any request can get, is on Errors and limits.