---
title: "Create an opt-out"
description: "Opts a number out so messages are no longer sent to it. The number is added to your default\nopt-out list unless you pass `opt_out_list_id` or `opt_out_list_name` (not both). A list\nnamed by `opt_out_list_name` is created if it does not exist. If the number is already opted\nout, the existing opt-out is returned.\n"
version: "v2"
---

> Documentation Index
> Fetch the complete documentation index at: https://dev.touchsms.com.au/llms.txt
> Use this file to discover all available pages before exploring further.

Path: touchSMS API › Opt-outs

`POST /v2/optouts`

Opts a number out so messages are no longer sent to it. The number is added to your default
opt-out list unless you pass `opt_out_list_id` or `opt_out_list_name` (not both). A list
named by `opt_out_list_name` is created if it does not exist. If the number is already opted
out, the existing opt-out is returned.

## Authentication

- `basicAuth`, http, header `Authorization`

## Request body

_Required._

- `number` (`createOptout.number`; string, required) — Number to opt out, ideally in E.164 format.
- `opt_out_list_id` (`createOptout.opt_out_list_id`; integer, optional) — Add to this opt-out list. Cannot be combined with `opt_out_list_name`.
- `opt_out_list_name` (`createOptout.opt_out_list_name`; string, optional) — Add to the list with this name, creating it if needed. Cannot be combined with `opt_out_list_id`.
  - maxLength 255

## Example request

```json
{
  "number": "+61412345678",
  "opt_out_list_name": "Marketing Opt-Outs"
}
```

## Code samples

### cURL

```bash
curl --request POST \
  --url https://app.touchsms.com.au/api/v2/optouts \
  --header 'Authorization: Basic <credentials>' \
  --header 'Content-Type: application/json' \
  --data '
{
  "number": "+61412345678",
  "opt_out_list_name": "Marketing Opt-Outs"
}
'
```

### TypeScript

```typescript
const url = 'https://app.touchsms.com.au/api/v2/optouts';
const options = {
  method: 'POST',
  headers: {'Content-Type': 'application/json', Authorization: 'Basic <credentials>'},
  body: JSON.stringify({number: '+61412345678', opt_out_list_name: 'Marketing Opt-Outs'})
};

fetch(url, options)
  .then(res => res.json())
  .then(json => console.log(json))
  .catch(err => console.error(err));
```

### Python

```python
import requests

url = "https://app.touchsms.com.au/api/v2/optouts"

payload = {
    "number": "+61412345678",
    "opt_out_list_name": "Marketing Opt-Outs"
}
headers = {
    "Content-Type": "application/json",
    "Authorization": "Basic <credentials>"
}

response = requests.post(url, json=payload, headers=headers)

print(response.text)
```

## Responses

### 200

The number was already opted out; the existing opt-out is returned.

#### Example

```json
{
  "data": {
    "id": 12345,
    "user": 5678,
    "number": "+61412345678",
    "method": "reply",
    "opt_out_list": {
      "id": 1,
      "name": "Default Opt-Out List",
      "is_default": true
    },
    "updated": "2024-03-11T02:14:07.000000Z",
    "created": "2024-03-11T02:14:07.000000Z"
  }
}
```

- `data` (`createOptout.response.200.data`; object, optional)
  - example `{"id":12345,"user":5678,"number":"+61412345678","method":"reply","opt_out_list":{"id":1,"name":"Default Opt-Out List","is_default":true},"updated":"2024-03-11T02:14:07.000000Z","created":"2024-03-11T02:14:07.000000Z"}`
  - `id` (`createOptout.response.200.data.id`; integer, optional)
  - `user` (`createOptout.response.200.data.user`; integer, optional) — ID of the user that owns the opt-out.
  - `number` (`createOptout.response.200.data.number`; string, optional) — Opted-out number in E.164 format.
  - `method` (`createOptout.response.200.data.method`; string | null, optional) — How the opt-out was created.
    - one of `"manual"`, `"import"`, `"link"`, `"reply"`, `"chat"`, `"api"`, `"workflow"`, `null`
  - `opt_out_list` (`createOptout.response.200.data.opt_out_list`; object, optional) — The list this opt-out belongs to.
    - `id` (`createOptout.response.200.data.opt_out_list.id`; integer, optional)
    - `name` (`createOptout.response.200.data.opt_out_list.name`; string, optional)
    - `is_default` (`createOptout.response.200.data.opt_out_list.is_default`; boolean, optional)
  - `updated` (`createOptout.response.200.data.updated`; string, optional)
    - format `date-time`
  - `created` (`createOptout.response.200.data.created`; string, optional)
    - format `date-time`

### 201

Opt-out created.

#### Example

```json
{
  "data": {
    "id": 12345,
    "user": 5678,
    "number": "+61412345678",
    "method": "reply",
    "opt_out_list": {
      "id": 1,
      "name": "Default Opt-Out List",
      "is_default": true
    },
    "updated": "2024-03-11T02:14:07.000000Z",
    "created": "2024-03-11T02:14:07.000000Z"
  }
}
```

- `data` (`createOptout.response.201.data`; object, optional)
  - example `{"id":12345,"user":5678,"number":"+61412345678","method":"reply","opt_out_list":{"id":1,"name":"Default Opt-Out List","is_default":true},"updated":"2024-03-11T02:14:07.000000Z","created":"2024-03-11T02:14:07.000000Z"}`
  - `id` (`createOptout.response.201.data.id`; integer, optional)
  - `user` (`createOptout.response.201.data.user`; integer, optional) — ID of the user that owns the opt-out.
  - `number` (`createOptout.response.201.data.number`; string, optional) — Opted-out number in E.164 format.
  - `method` (`createOptout.response.201.data.method`; string | null, optional) — How the opt-out was created.
    - one of `"manual"`, `"import"`, `"link"`, `"reply"`, `"chat"`, `"api"`, `"workflow"`, `null`
  - `opt_out_list` (`createOptout.response.201.data.opt_out_list`; object, optional) — The list this opt-out belongs to.
    - `id` (`createOptout.response.201.data.opt_out_list.id`; integer, optional)
    - `name` (`createOptout.response.201.data.opt_out_list.name`; string, optional)
    - `is_default` (`createOptout.response.201.data.opt_out_list.is_default`; boolean, optional)
  - `updated` (`createOptout.response.201.data.updated`; string, optional)
    - format `date-time`
  - `created` (`createOptout.response.201.data.created`; string, optional)
    - format `date-time`

### 400

The number could not be parsed.

### 401

Missing, malformed or invalid API credentials.

#### Example

```json
{
  "message": "Unauthenticated."
}
```

- `message` (`createOptout.response.401.message`; string, required)

### 422

The request failed validation. `errors` lists the messages for each invalid field.

#### Example

```json
{
  "message": "The messages field is required.",
  "errors": {
    "messages": [
      "The messages field is required."
    ]
  }
}
```

- `message` (`createOptout.response.422.message`; string, optional)
- `errors` (`createOptout.response.422.errors`; map<array<string>>, optional)


Source: https://dev.touchsms.com.au/api/optouts/create/index.md
