Skip to content
Codelia SMSDocs
Guides: Campaigns and templates

Campaigns and templates

A campaign sends one message to your lists, to numbers you give it, or both. Use a campaign when you send to more than 500 numbers (the most one message takes), to a list, with each person's own details in the text, or at a later time. Sending a message does none of those: it has no placeholders and no schedule.

Send a campaign

Send the template and who gets it to POST /organizations/{accountId}/messages/campaigns. The API key needs messages:send:

curl -X POST "https://api.sms.codelia.studio/v1/organizations/$CODELIA_ACCOUNT_ID/messages/campaigns" \
  -H "Authorization: Bearer $CODELIA_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: spring-sale-2026" \
  -d '{
  "from": "InfoSMS",
  "body": "Hi {{firstName}}, our spring sale starts Saturday: 20% off everything.",
  "recipients": [
    {
      "to": "+442079460123",
      "variables": {
        "firstName": "Ada"
      }
    },
    {
      "to": "+442079460124",
      "variables": {
        "firstName": "Grace"
      }
    }
  ]
}'
FieldWhat to send
recipientsUp to 5,000 numbers per request, each { "to": …, "variables": { … }, "language": … }. Only to is required.
listIdsUp to 25 lists. Send recipients, listIds, or both.
body or bodiesThe template, up to 1,600 characters, or one per language, as when you send a message.
fromThe sender name. Leave it out to use your default.
scheduledAtSend later. See Schedule it.
metadataYour own keys and text values, kept on the campaign. Keys starting with audience. are refused with reserved_metadata_key.
defaultsFallback values for placeholders, like { "firstName": "there" }. See Placeholders.

The answer is 202 with the campaign: keep its id. The messages go out in the background.

Once your lists and recipients are put together, the campaign can reach up to 100,000 people; past that, it's refused with too_many_recipients. A number that is on a list and in recipients gets one message.

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

Placeholders

Write {{firstName}} in the template, and each person gets their own value in its place. Spaces inside the braces are allowed ({{ firstName }}). A name is up to 32 letters, digits and underscores, starts with a letter or an underscore, and is case-sensitive: {{FirstName}} isn't {{firstName}}.

Where the values come from:

  • Someone on a list is filled in from their contact: firstName, lastName, email, birthday, and any key in the contact's metadata, such as a column from an import.
  • A number in recipients is filled in from its own variables, even when a contact has the same number.
  • A number in recipients that is also on one of the lists takes its variables when the entry has a variables key. With no variables key at all, it's filled in from the contact. "variables": {} means no values at all.

A contact's birthday fills {{birthday}} as the day and month, two digits each, like 14/03, and never shows the year. If a contact has no birthday set but their metadata has a birthday key, such as a column from an import, the placeholder uses that value. It reads as day/month, like 14/03, when it can be read as a date in one of the formats an import reads, and as written otherwise.

To fill in people who have no value, send defaults: one fallback per placeholder name, like { "firstName": "there" }, so Hi {{firstName}} reads Hi there for them. A default is used when a person's value is missing, empty or only spaces, and never replaces a value they have. A default for a placeholder none of your texts use is ignored. Each default is up to 200 characters, and a campaign takes up to 50.

Without a default, a value can be an empty string: its placeholder is replaced with nothing. A missing value fails that person's message with missing_variable.

When a person's text can't be filled in, their message fails with an errorCode, isn't charged, and everyone else's message still goes out:

errorCodeWhat happened
missing_variableThe template uses a placeholder this person has no value for.
unknown_variableTheir variables has a key the template doesn't use. Check it for a typo.
invalid_variable_nameA key in their variables isn't a valid placeholder name.
value_too_longA value is longer than 200 characters.
rendered_text_emptyThe filled-in text is empty.
rendered_text_too_longThe filled-in text, with the unsubscribe line, is longer than 1,600 characters.

Each person is counted and priced on their own filled-in text, so one long name can put their message into a second part.

Lists

Send to a list by its id in listIds. A smart list reaches whoever matches its filter when the campaign is accepted, or, for a scheduled campaign, when it sends. Creating lists, importing contacts and adding people to a list are on Contacts and lists.

Price it first

POST /organizations/{accountId}/messages/campaigns/quote takes the same body as POST …/messages/campaigns, fills in and prices every person the same way, and never sends or charges. The API key needs messages:read:

curl -X POST "https://api.sms.codelia.studio/v1/organizations/$CODELIA_ACCOUNT_ID/messages/campaigns/quote" \
  -H "Authorization: Bearer $CODELIA_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "from": "InfoSMS",
  "body": "Hi {{firstName}}, our spring sale starts Saturday: 20% off everything.",
  "recipients": [
    {
      "to": "+442079460123",
      "variables": {
        "firstName": "Ada"
      }
    },
    {
      "to": "+442079460124",
      "variables": {
        "firstName": "Grace"
      }
    }
  ]
}'

The answer has the same shape as pricing a message. People whose text can't be filled in, and numbers that aren't valid or are in a country your sender name or our prices don't cover, are counted in unpriceableCount. Numbers in a country your organization can't send to yet are left out of the price and counted in notAllowedCount. People who opted out and numbers we can't send to are still priced, so sending can cost less than the estimated cost.

How it's charged

The whole estimated cost is taken from your balance once, when the campaign is accepted. If your balance can't cover it, the campaign is refused with 402 and insufficient_credits, and nothing is sent. A large campaign sent now is accepted shortly after you send it, so a shortfall found then comes back differently: see Large campaigns. A campaign that was accepted never stops halfway for lack of credit.

You get money back for:

  • someone who opted out after the campaign was accepted, just before their message was handed to the network;
  • messages the network refused to take;
  • messages not yet sent when you cancel the campaign;
  • messages not yet sent when a transactional campaign stops because your organization's access to transactional messages was turned off.

A message the network took keeps its charge, even if it's never delivered.

Large campaigns

A campaign you send now to more than 500 people, counting each list's members before duplicates are removed, is accepted shortly after the request is answered. The answer is 202 at once, with status queued. Until it's accepted, recipientCount is an estimate that can only go down, and totalEstimatedMicros is 0. Follow it as any campaign, or listen for the campaign.failed webhook.

It's still refused when you send it, as any campaign is, for a problem with the request itself (such as unknown_list, unapproved_sender_id or reserved_metadata_key), for a sending limit your organization has already reached (403, sending_limit_reached), and when your balance is at or below zero and auto top-up can't add to it (402, insufficient_credits).

Anything found once it's accepted makes the campaign failed, with a failureReason, and nothing is charged:

  • If your balance doesn't cover it, auto top-up is charged once and the campaign waits about 10 minutes for the credit, with pendingReason: "awaiting_credit". If the credit hasn't arrived by then, or auto top-up can't be charged, it fails with insufficient_credits.
  • If it would pass a sending limit, it fails with sending_limit_reached.
  • If its lists come to more than 100,000 people, it fails with too_many_recipients.
  • If no one is left to send to but numbers we can't send to, it fails with all_recipients_unsupported, or all_recipients_not_allowed if one of them is in a country your organization can't send to yet.
  • If we couldn't process it within an hour, it fails with processing_failed.

A campaign that failed this way can be sent again, unless it failed with all_recipients_unsupported or unknown_list.

Follow it

Check a campaign with GET /organizations/{accountId}/messages/campaigns/{campaignId}, where {campaignId} is the id in the answer when you sent it:

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

Poll it while status is queued or sending, or use webhooks to be told when it finishes. The counts:

FieldWhat it counts
recipientCountEveryone the campaign was for, once lists and recipients are put together.
sentCountMessages handed to the network.
skippedCountPeople who weren't sent it because they opted out, their number is one we can't send to, or it's in a country your organization can't send to yet.
failedCountEvery other message that failed, including people whose text couldn't be filled in.
deliveredCountMessages a delivery report had confirmed when the campaign finished.

While a campaign is sending, the counts go up as each batch of its messages is handed to the network. They're fixed when the campaign finishes. A delivery report that arrives later doesn't change them, or the status. To see what happened to each person, list the campaign's messages with GET /organizations/{accountId}/messages/campaigns/{campaignId}/messages, or follow the message.delivered and message.failed webhook events.

A campaign's status is one of:

statusWhat it means
scheduledWaiting for its time. Nothing is charged yet.
queuedWaiting to start. Its messages haven't started going out. A campaign sent now to more than 500 people is queued for a short while before it's accepted and charged.
sendingIts messages are being handed to the network.
completedFinished, and every message was handed to the network, apart from people it skipped. It doesn't mean every message was delivered.
partial_failureFinished, and failedCount is more than 0: some messages were handed over and some failed.
failedNothing was handed to the network. That includes a campaign where everyone opted out: it sends campaign.failed, and nothing is charged.
cancelledYou canceled it.

People who opted out, numbers we can't send to (errorCode unsupported_number) and numbers in a country your organization can't send to yet (errorCode country_not_allowed) are counted in skippedCount, not failedCount, so a campaign whose only misses are these is completed. If one of the numbers was either of the last two and no one else is left to send to, the campaign is refused with a 400 when you send or schedule it: 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. A campaign where everyone opted out finishes failed. A failed placeholder is a failure: it counts in failedCount.

A campaign that couldn't send to anyone says why in failureReason, and nothing is charged for it:

  • all_recipients_opted_out: everyone it was for had opted out.
  • no_sendable_recipients: nobody could be sent the message, and at least one person failed for a reason other than opting out. Each person's message has an errorCode that says why.
  • all_recipients_not_allowed or all_recipients_unsupported: when a scheduled campaign was due, or once a large campaign sent now was accepted, one of its numbers was in a country your organization can't send to yet, or was a number we can't send to, and no one else was left to send to. Any other campaign you send now with such numbers is refused with a 400 instead.
  • A scheduled campaign that couldn't go out when it was due, or a large campaign sent now that couldn't go out once it was accepted, has a code like insufficient_credits, unapproved_sender_id, sender_id_required (it was sent without from and your organization no longer has a default sender), organization_suspended, sending_limit_reached, no_recipients, unknown_list, unsupported_filter (a smart list's filter can't be read), default_variant_required or too_many_recipients. A large campaign can also have processing_failed, when we couldn't process it within an hour.
  • feature_not_enabled: a scheduled transactional campaign was due, and your organization wasn't approved for transactional messages. Once it is, it can be sent again. See Transactional messages.

One reason is set on a campaign that sent to some people: a transactional campaign stops if your organization's access to transactional messages is turned off while it's sending. It finishes partial_failure, or failed if nothing had gone yet, with failureReason: "feature_not_enabled". The messages not yet sent have errorCode campaign_cancelled and are refunded. See If access is turned off while it's sending.

Otherwise failureReason is null: a campaign that sent to anyone records its failures on each message. New reasons can be added, so treat one you don't know as a failure.

A sending campaign with stalled: true stopped part-way. It's picked up again on its own; to send the rest now, call POST /organizations/{accountId}/messages/campaigns/{campaignId}/resume. Nothing is charged again, and nobody who already got the message gets it twice.

Schedule it

Add scheduledAt, between 5 minutes and 90 days ahead, and the campaign is created scheduled:

  • Scheduling needs auto top-up, set up in the portal. Without it, the answer is 409 with auto_topup_required.
  • Nothing is charged when you schedule. The campaign is priced and charged when it sends, at that day's prices, and a smart list reaches whoever matches then.
  • If your balance is short when it's due, it tries again every 2 minutes for up to 30 minutes. After that it becomes failed, with failureReason: "insufficient_credits".
  • If, when it's due, no one is left to send to but numbers we can't send to (and people who opted out), it becomes failed, with failureReason: "all_recipients_unsupported", and nothing is charged. If one of those numbers is in a country your organization can't send to yet, the failureReason is all_recipients_not_allowed instead.
  • A campaign that failed when it was due can be sent again now.

Move it with PATCH /organizations/{accountId}/messages/campaigns/{campaignId} and a new scheduledAt, while it's still scheduled. Rescheduling needs auto top-up, as scheduling does. Rescheduling a transactional campaign also needs your organization approved for transactional messages; otherwise it's refused with 403 and feature_not_enabled, and the campaign keeps its old time. Cancel it with POST /organizations/{accountId}/messages/campaigns/{campaignId}/cancel. You can cancel a scheduled or queued campaign, or a stalled one; messages already handed to the network can't be called back. Canceling or resuming a transactional campaign doesn't need approval.

Repeat it

POST /organizations/{accountId}/messages/recurring sends a campaign on a rule: weekly on the days you choose, monthly on a day of the month or its last day, or yearly on a date, each at a time in a time zone you choose. Its send is the body a campaign takes, without scheduledAt. About 45 minutes before each time, it creates a scheduled campaign, priced and charged when it sends.

Creating or resuming one needs auto top-up. Changing it doesn't. Recurring campaigns has the rule, what it creates, and how to change, pause or delete one.

Find your campaigns

  • List them with GET /organizations/{accountId}/messages/campaigns, newest first. view=scheduled gives only those waiting for their time, soonest first; view=sent gives every other one, and with includeCancelled set to false it leaves out the canceled ones. recurringCampaignId narrows the list to one recurring campaign's. It pages by cursor: see Paging through results.
  • Count them with GET /organizations/{accountId}/messages/campaigns/summary: scheduledCount, and in notSent, how many scheduled campaigns, and large campaigns sent now, failed to go out in the last 7 days, with the latest one's ID.
  • Break an audience down by language before you write the text, with POST /organizations/{accountId}/messages/audience and the recipients and listIds you'll send to. It counts who would get each language, and prices nothing. Add fields, up to 20 placeholder names, to see in fields how many people have their own value for each, and in sample up to 8 people with their values, with your defaults filled in where they have none.

Each needs messages:read.

Send to the same people again

POST /organizations/{accountId}/messages/campaigns/{campaignId}/resend sends the campaign again as a new campaign, priced and charged at today's prices. It sends each person the text they got last time. With "onlyFailed": true, it sends only to people whose message has status failed, rejected or undelivered, not to people who were skipped because they opted out, their number is one we can't send to, or it's in a country your organization can't send to yet. Anyone who has opted out since is skipped too, as on any marketing campaign.

It also leaves out people whose placeholders couldn't be filled in: their message's errorCode is missing_variable, unknown_variable, invalid_variable_name, value_too_long, rendered_text_empty or rendered_text_too_long. Their stored text is usually the template itself, so it would go out with its {{placeholders}} as written, and the same values would fail the same way. Send them a new campaign with the missing values, or with defaults, instead. If no one is left to send to, the answer is 409 with nothing_to_resend, and nothing is charged.

Send a refused campaign again

A campaign that failed before it was accepted has status failed and a failureReason: a scheduled campaign that failed when it was due, or a large campaign sent now that failed once it was accepted. Once you've fixed the reason, for example by topping up, send it now with POST /organizations/{accountId}/messages/campaigns/{campaignId}/send-again. The API key needs messages:send, and the request takes an Idempotency-Key header.

A campaign that failed with all_recipients_unsupported can't be sent again, because we can't send to those numbers. If you think we should be able to, email hello@codelia.studio. One that failed with unknown_list can't be sent again either, because a deleted list doesn't come back: send a new campaign to the lists you have now. Both are refused with 409 and not_sendable_again. So is a campaign of up to 500 people you sent now that no one could be sent to: it was accepted when you sent it, and each person's message says why, so send to the same people again instead.

It's sent as a new campaign, with the same lists, numbers and text as when you scheduled or sent it. The lists are read again, so a smart list reaches whoever matches now, and it's priced and charged at today's prices. The answer is 202 with the new campaign. The failed campaign doesn't change.

To see what it would cost first, call GET /organizations/{accountId}/messages/campaigns/{campaignId}/send-again/quote. It prices the campaign as it would go out now, sends nothing and charges nothing, and needs messages:read.

It's checked like any campaign you send now, so it can be refused for the same reasons. A refused attempt doesn't count, so you can fix the reason and try again. Neither does a new campaign that is itself large and fails before it's accepted: the failed campaign's sentAgainAt and sentAgainCampaignId go back to null, and you can send it again.

A failed campaign can be sent again once. After that, it's refused with 409 and not_sendable_again, and sentAgainCampaignId is the campaign it was sent as: nothing more is sent or charged. While that campaign is still being created, sentAgainCampaignId is null. If that attempt was cut off and created nothing, you can send it again 15 minutes after it started. Any other campaign is refused the same way: to repeat a campaign that went out, use Send to the same people again.

When it's refused

A refused campaign sends nothing and charges nothing.

StatuserrorWhat happenedWhat to do
400too_many_recipientsYour lists and recipients come to more than 100,000 people.Split the audience across campaigns.
400unknown_listA list in listIds doesn't exist in your organization.Check the list IDs.
400no_recipientsThe lists and recipients came to nobody.Add people to the list, or send recipients.
400all_recipients_unsupportedWe can't send to any of the numbers. Anyone else in the request opted out. Nothing was sent, scheduled 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 is left to send to. Nothing was sent, scheduled or charged.Email hello@codelia.studio to send to them.
400reserved_metadata_keyA metadata key starts with audience..Rename the key.
400invalid_schedulescheduledAt is less than 5 minutes or more than 90 days ahead.Choose a time in that window, or leave it out to send now.
402insufficient_creditsYour balance can't cover the estimated cost.Top up, then send again with a new Idempotency-Key.
409auto_topup_requiredYou scheduled a campaign, and auto top-up isn't ready to pay for it.Set up auto top-up in the portal, then schedule it again.
409unsupported_filterA smart list's filter can't be read.Edit the list to rebuild its filter.

A large campaign sent now isn't refused here with too_many_recipients, all_recipients_unsupported, all_recipients_not_allowed, or insufficient_credits for a balance above zero that's short: those arrive later as its failureReason.

A campaign is refused with sender_id_required, unapproved_sender_id, default_variant_required or organization_suspended for the same reasons as a message. A number the campaign can't send to (invalid_recipient, sender_country_not_allowed, no_rate_for_country, country_disabled or currency_mismatch) doesn't refuse it: that person's message fails with the code as its errorCode, and isn't charged. Every code is on Errors and limits.