# Broadcasts and audiences

A broadcast is one email for a whole audience. Each person gets their own copy, with their name, what you know about them and their own unsubscribe link.

## Make an audience

An audience is a group of contacts. A broadcast goes to one audience, and a contact can be in many.

cURL:

```bash
curl -X POST "$OPENSEND_API_URL/v1/audiences" \
  -H "Authorization: Bearer $OPENSEND_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "name": "Beta testers"
}'
```

TypeScript:

```ts
await opensend.audiences.create({
  name: 'Beta testers',
})
```

CLI:

```bash
opensend audiences create --name 'Beta testers'
```

MCP:

```json
{
  "name": "audiences_create",
  "arguments": {
    "name": "Beta testers"
  }
}
```

## Add contacts

A contact is a person of the workspace: an address, with a first and last name when you know them. The workspace holds an address once, so a second contact with it is refused with `contact_taken`. Name the audiences to put a new contact in with `audienceIds`.

cURL:

```bash
curl -X POST "$OPENSEND_API_URL/v1/contacts" \
  -H "Authorization: Bearer $OPENSEND_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "email": "grace@example.com",
  "firstName": "Grace",
  "audienceIds": [
    "aud_01"
  ]
}'
```

TypeScript:

```ts
await opensend.contacts.create({
  email: 'grace@example.com',
  firstName: 'Grace',
  audienceIds: ['aud_01'],
})
```

CLI:

```bash
opensend contacts create \
  --email grace@example.com \
  --first-name Grace \
  --audience-ids aud_01
```

MCP:

```json
{
  "name": "contacts_create",
  "arguments": {
    "email": "grace@example.com",
    "firstName": "Grace",
    "audienceIds": [
      "aud_01"
    ]
  }
}
```

Someone who is a contact already joins another audience with one call, by id or by address. Taking them out of an audience leaves them a contact.

cURL:

```bash
curl -X POST "$OPENSEND_API_URL/v1/audiences/aud_02/contacts/ada%40example.com" \
  -H "Authorization: Bearer $OPENSEND_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{}'
```

TypeScript:

```ts
await opensend.audiences.join('aud_02', {
  contact: 'ada@example.com',
})
```

CLI:

```bash
opensend audiences join aud_02 --contact ada@example.com
```

MCP:

```json
{
  "name": "audiences_join",
  "arguments": {
    "id": "aud_02",
    "contact": "ada@example.com"
  }
}
```

A contact is `subscribed` or `unsubscribed`, in every audience at once. Only subscribed contacts get a broadcast. A plan allows a number of contacts, counted once each however many audiences they are in, and one more is refused with `contact_limit`.

cURL:

```bash
curl "$OPENSEND_API_URL/v1/contacts/ada%40example.com" \
  -H "Authorization: Bearer $OPENSEND_API_KEY"
```

TypeScript:

```ts
await opensend.contacts.get('ada@example.com')
```

CLI:

```bash
opensend contacts get ada@example.com
```

MCP:

```json
{
  "name": "contacts_get",
  "arguments": {
    "id": "ada@example.com"
  }
}
```

## What you know about a contact

A contact holds `properties`: values of your own by name, such as a plan. Give a property a type and a fallback and it is checked from then on: a value of another type is refused, and `{{plan}}` in a broadcast is filled with the value of each contact, or with the fallback for a contact who has none.

cURL:

```bash
curl -X POST "$OPENSEND_API_URL/v1/contact-properties" \
  -H "Authorization: Bearer $OPENSEND_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "key": "company",
  "type": "string",
  "fallback": "your company"
}'
```

TypeScript:

```ts
await opensend.contactProperties.create({
  key: 'company',
  type: 'string',
  fallback: 'your company',
})
```

CLI:

```bash
opensend contact-properties create \
  --key company \
  --type string \
  --fallback 'your company'
```

MCP:

```json
{
  "name": "contact_properties_create",
  "arguments": {
    "key": "company",
    "type": "string",
    "fallback": "your company"
  }
}
```

A key you never defined still works: it takes any value and has no fallback.

## Let readers choose

A topic is a kind of mail a reader can say yes or no to, such as product news. `defaultSubscription` says what holds for someone who never chose: `opt_in`, they get it, or `opt_out`, they only get it after they say yes.

cURL:

```bash
curl -X POST "$OPENSEND_API_URL/v1/topics" \
  -H "Authorization: Bearer $OPENSEND_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "name": "Product news",
  "description": "What we shipped, once a month."
}'
```

TypeScript:

```ts
await opensend.topics.create({
  name: 'Product news',
  description: 'What we shipped, once a month.',
})
```

CLI:

```bash
opensend topics create \
  --name 'Product news' \
  --description 'What we shipped, once a month.'
```

MCP:

```json
{
  "name": "topics_create",
  "arguments": {
    "name": "Product news",
    "description": "What we shipped, once a month."
  }
}
```

Give a broadcast a `topicId` and it only reaches the subscribed contacts of its audience who get that topic. Its unsubscribe link then takes a reader out of the topic and not out of everything, and the page behind the link lists the topics with a tick each. What one contact chose is `topics` on the contact.

cURL:

```bash
curl -X PATCH "$OPENSEND_API_URL/v1/contacts/ada%40example.com" \
  -H "Authorization: Bearer $OPENSEND_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "status": "unsubscribed"
}'
```

TypeScript:

```ts
await opensend.contacts.update('ada@example.com', {
  status: 'unsubscribed',
})
```

CLI:

```bash
opensend contacts update ada@example.com --status unsubscribed
```

MCP:

```json
{
  "name": "contacts_update",
  "arguments": {
    "id": "ada@example.com",
    "status": "unsubscribed"
  }
}
```

## Write the broadcast

A broadcast starts as a draft: a name, an audience, a sender, a subject and a body, and a reply address when replies should not go to the sender. A broadcast with no reply address of its own gets the default of the workspace. Nothing is sent until you send it.

cURL:

```bash
curl -X POST "$OPENSEND_API_URL/v1/broadcasts" \
  -H "Authorization: Bearer $OPENSEND_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "name": "December product update",
  "audienceId": "aud_01",
  "subject": "What is new"
}'
```

TypeScript:

```ts
await opensend.broadcasts.create({
  name: 'December product update',
  audienceId: 'aud_01',
  subject: 'What is new',
})
```

CLI:

```bash
opensend broadcasts create \
  --name 'December product update' \
  --audience-id aud_01 \
  --subject 'What is new'
```

MCP:

```json
{
  "name": "broadcasts_create",
  "arguments": {
    "name": "December product update",
    "audienceId": "aud_01",
    "subject": "What is new"
  }
}
```

These variables are filled in for each contact.

| Variable | Filled with |
| --- | --- |
| `{{first_name}}` | The first name of the contact. |
| `{{last_name}}` | The last name of the contact. |
| `{{email}}` | The address of the contact. |
| `{{unsubscribe_url}}` | The unsubscribe link of this contact. |
| `{{plan}}` | A property of the contact, or its fallback. |

What a contact wrote is escaped before it goes into the HTML. When the body has no `{{unsubscribe_url}}`, an unsubscribe link is added at the end.

A draft or a scheduled broadcast can still be changed.

cURL:

```bash
curl -X PATCH "$OPENSEND_API_URL/v1/broadcasts/bc_05" \
  -H "Authorization: Bearer $OPENSEND_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "subject": "What is new in December"
}'
```

TypeScript:

```ts
await opensend.broadcasts.update('bc_05', {
  subject: 'What is new in December',
})
```

CLI:

```bash
opensend broadcasts update bc_05 --subject 'What is new in December'
```

MCP:

```json
{
  "name": "broadcasts_update",
  "arguments": {
    "id": "bc_05",
    "subject": "What is new in December"
  }
}
```

## Build it from blocks

A broadcast does not have to be written as HTML. Give `design` in place of `html` and `text`, and both are made from it, with markup that mail apps accept. It is the design a template takes: a `body` of blocks, a `style` for the whole message, a `preview` and the `fonts` it loads. [Templates](https://opensend.sh/docs/templates) shows one and lists the blocks.

- The variables of a broadcast stand anywhere in the `text` of a block. In `href` and `src` a variable has to be the whole value.
- For an unsubscribe link of your own, give a `Link` or a `Button` the `href` `{{unsubscribe_url}}`. Without one, a link is added at the end of the message.
- `html` and `text` belong to the design from then on. Sending one of them to a broadcast built from blocks is refused with `broadcast_designed`. Send `design` again to change it, or `design: null` to turn it into a broadcast written as HTML, which keeps the HTML and the plain text it has.
- A design with no blocks is not sent: `broadcasts.send` answers `email_no_body`.

## Try it first

A test sends the draft to addresses you give, with "[Test]" before the subject, and leaves the broadcast as it is. An address that is a contact of the audience gets its own name and unsubscribe link, and any other gets the message with the names empty. The draft needs a sender, a subject and a message.

cURL:

```bash
curl -X POST "$OPENSEND_API_URL/v1/broadcasts/bc_05/test" \
  -H "Authorization: Bearer $OPENSEND_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "to": "you@example.com"
}'
```

TypeScript:

```ts
await opensend.broadcasts.test('bc_05', {
  to: 'you@example.com',
})
```

CLI:

```bash
opensend broadcasts test bc_05 --to you@example.com
```

MCP:

```json
{
  "name": "broadcasts_test",
  "arguments": {
    "id": "bc_05",
    "to": "you@example.com"
  }
}
```

A copy of a broadcast is a new draft with the same audience, sender, subject and message. It works on one that was already sent, which is how you send it again.

cURL:

```bash
curl -X POST "$OPENSEND_API_URL/v1/broadcasts/bc_04/duplicate" \
  -H "Authorization: Bearer $OPENSEND_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{}'
```

TypeScript:

```ts
await opensend.broadcasts.duplicate('bc_04')
```

CLI:

```bash
opensend broadcasts duplicate bc_04
```

MCP:

```json
{
  "name": "broadcasts_duplicate",
  "arguments": {
    "id": "bc_04"
  }
}
```

## Send it

Send the broadcast now, or give `scheduledAt` to start at a later time. It goes to everyone who is subscribed at that moment.

cURL:

```bash
curl -X POST "$OPENSEND_API_URL/v1/broadcasts/bc_05/send" \
  -H "Authorization: Bearer $OPENSEND_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{}'
```

TypeScript:

```ts
await opensend.broadcasts.send('bc_05')
```

CLI:

```bash
opensend broadcasts send bc_05
```

MCP:

```json
{
  "name": "broadcasts_send",
  "arguments": {
    "id": "bc_05"
  }
}
```

- A large broadcast goes out in parts. Its status is `sending` until the last part is out, then `sent`.
- `recipients` is how many people it is for, `delivered` how many copies were delivered, and `failed` how many the platform refused.
- `email_no_subject` and `email_no_body`: the broadcast has no subject yet, or no message.
- `broadcast_empty`: nobody is subscribed to the audience.
- `broadcast_sent`: it has already been sent, so it cannot be sent or changed again.

## Unsubscribing

The unsubscribe link opens a page of yours on the API, where the reader confirms. The contact then becomes `unsubscribed` and gets no more broadcasts.

The page takes its title, message, button and accent colour from the workspace. Change them with `unsubscribePage`.

cURL:

```bash
curl "$OPENSEND_API_URL/v1/workspace" \
  -H "Authorization: Bearer $OPENSEND_API_KEY"
```

TypeScript:

```ts
await opensend.workspace.get()
```

CLI:

```bash
opensend workspace get
```

MCP:

```json
{
  "name": "workspace_get",
  "arguments": {}
}
```
