Sending messages
POST / 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."
}'const response = await fetch(
`https://api.sms.codelia.studio/v1/organizations/${process.env.CODELIA_ACCOUNT_ID}/messages`,
{
method: "POST",
headers: {
Authorization: `Bearer ${process.env.CODELIA_API_KEY}`,
"Content-Type": "application/json",
"Idempotency-Key": "order-1042-ready",
},
body: JSON.stringify({
"to": [
"+442079460123"
],
"from": "InfoSMS",
"body": "Your order 1042 is ready to collect."
}),
},
);
console.log(response.status);
console.log(JSON.stringify(await response.json(), null, 2));The body takes:
| Field | What to send |
|---|---|
to | 1 to 500 numbers in international format, like +442079460123. The same number, written the same way, is sent once. |
body | The text, up to 1,600 characters. Send body or bodies, never both. |
bodies | One text per language, instead of body. See Send in each person's language. |
from | The sender name. Leave it out to use your default. |
type | marketing (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_ 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_, for either type of message. So is a number in a country your organization can't send to yet, with errorCode: "country_. 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_ if one of the numbers is in such a country, otherwise all_. 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 είναι έτοιμη για παραλαβή."
}
}'const response = await fetch(
`https://api.sms.codelia.studio/v1/organizations/${process.env.CODELIA_ACCOUNT_ID}/messages`,
{
method: "POST",
headers: {
Authorization: `Bearer ${process.env.CODELIA_API_KEY}`,
"Content-Type": "application/json",
"Idempotency-Key": "order-1042-ready-languages",
},
body: JSON.stringify({
"to": [
{
"to": "+442079460123",
"language": "el"
},
"+442079460124"
],
"from": "InfoSMS",
"bodies": {
"en": "Your order 1042 is ready to collect.",
"el": "Η παραγγελία σας 1042 είναι έτοιμη για παραλαβή."
}
}),
},
);
console.log(response.status);
console.log(JSON.stringify(await response.json(), null, 2));Each number gets the text for its language, picked in this order:
- the
languageon its entry into; - otherwise, the preferred language of the contact with that number;
- 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_. 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:
- the
fromin the request; - otherwise, your organization's default sender name;
- otherwise nothing, and the request is refused with
400andsender_.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_. 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"const response = await fetch(
`https://api.sms.codelia.studio/v1/organizations/${process.env.CODELIA_ACCOUNT_ID}/sender-ids`,
{
headers: {
Authorization: `Bearer ${process.env.CODELIA_API_KEY}`,
},
},
);
console.log(response.status);
console.log(JSON.stringify(await response.json(), null, 2));To ask for a new one, send its value to POST / 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 message | Parts | Why |
|---|---|---|
Your order 1042 is ready to collect. | 1 | 36 characters and the 41 of the unsubscribe line: 77, within 160. |
Your order 1042 is ready to collect 🎉 | 2 | The emoji counts as two and moves the message to 70 a part: 38 and 41 is 79. |
| A 136-character offer, with no emoji | 2 | 136 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 / takes the same body as POST …/, 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."
}'const response = await fetch(
`https://api.sms.codelia.studio/v1/organizations/${process.env.CODELIA_ACCOUNT_ID}/messages/quote`,
{
method: "POST",
headers: {
Authorization: `Bearer ${process.env.CODELIA_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
"to": [
"+442079460123"
],
"from": "InfoSMS",
"body": "Your order 1042 is ready to collect."
}),
},
);
console.log(response.status);
console.log(JSON.stringify(await response.json(), null, 2));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_, not_ or opted_. not_ means we didn't send it and didn't charge for it: its errorCode is unsupported_ or country_.
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 /, 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"const response = await fetch(
`https://api.sms.codelia.studio/v1/organizations/${process.env.CODELIA_ACCOUNT_ID}/messages?limit=20`,
{
headers: {
Authorization: `Bearer ${process.env.CODELIA_API_KEY}`,
},
},
);
console.log(response.status);
console.log(JSON.stringify(await response.json(), null, 2));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"const response = await fetch(
`https://api.sms.codelia.studio/v1/organizations/${process.env.CODELIA_ACCOUNT_ID}/messages/${process.env.MESSAGE_ID}/resend`,
{
method: "POST",
headers: {
Authorization: `Bearer ${process.env.CODELIA_API_KEY}`,
"Idempotency-Key": "order-1042-ready-again",
},
},
);
console.log(response.status);
console.log(JSON.stringify(await response.json(), null, 2));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_.
When it's refused
When a request is refused, nothing is sent or charged, except where the table says otherwise.
| Status | error | What happened | What to do |
|---|---|---|---|
| 400 | sender_ | No from, and your organization has no default sender name. | Send from, or choose a default sender name in the portal. |
| 400 | unapproved_ | The sender name isn't approved for your organization. | Use an approved sender name, or InfoSMS. |
| 400 | sender_ | The sender name can't be used for one of the numbers' countries. | Use another sender name for that country. |
| 400 | default_ | bodies has no text for your default language. | Add it to bodies. |
| 400 | invalid_ | A number couldn't be matched to a country. | Use international format, like +442079460123. |
| 400 | no_ | We don't send to one of the numbers' countries. | Remove those numbers. |
| 400 | all_ | Every number opted out of your marketing messages. | Don't retry them. |
| 400 | all_ | We 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. |
| 400 | all_ | Your 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. |
| 402 | insufficient_ | Your balance can't cover the messages. | Top up, then send again with a new Idempotency-Key. |
| 403 | country_ | Sending to one of the numbers' countries isn't available right now. | Remove those numbers, or try again later. |
| 502 | provider_ | The 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.