# Message a contact

`POST /v1/threads`

Required scope: `messages:write`

Send a text from a line, or an email from a mailbox. When a conversation with that number or address exists, the message goes into it and the answer is a 200. Texts are charged.

## Header parameters

- `Idempotency-Key` (string or null, optional)

## Request body (application/json)

- `attachments` (object[], optional) Default: `[]`.
  - `content_type` (string, required)
  - `data` (string, required)
  - `name` (string, required)
- `body` (string, required)
- `contact_id` (string, required)
- `html` (string or null, optional)
- `line_id` (string or null, optional)
- `mailbox_id` (string or null, optional)
- `subject` (string or null, optional)

## Responses

- `201`: Successful Response
- `400`: The request is not valid.
- `401`: Not signed in, or the credentials are not valid.
- `402`: The balance is too low.
- `403`: Not allowed to do this.
- `404`: Not found.
- `409`: Conflicts with the current state.
- `413`: The request body is too large.
- `415`: The content type is not supported.
- `422`: A value in the request is not valid.
- `429`: Too many requests. Wait for `Retry-After` seconds.
- `500`: Something went wrong on our side.
- `502`: A service Valeron depends on refused the request.
- `503`: A service Valeron depends on is unavailable.

## Examples

### Request

```bash title="cURL"
curl -X POST "https://api.valeron.io/v1/threads" \
  -H "Authorization: Bearer $VALERON_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "attachments": [
    {
      "content_type": "example",
      "data": "example",
      "name": "Jordan Rivera"
    }
  ],
  "body": "Thanks for your time today. Here is what we agreed.",
  "contact_id": "ct_01JCH8XFQH",
  "html": "example",
  "line_id": "lin_01JCP6WF0H",
  "mailbox_id": "mbx_01JRJX4977"
}'
```

```js title="JavaScript"
const response = await fetch("https://api.valeron.io/v1/threads", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.VALERON_API_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "attachments": [
      {
        "content_type": "example",
        "data": "example",
        "name": "Jordan Rivera"
      }
    ],
    "body": "Thanks for your time today. Here is what we agreed.",
    "contact_id": "ct_01JCH8XFQH",
    "html": "example",
    "line_id": "lin_01JCP6WF0H",
    "mailbox_id": "mbx_01JRJX4977"
  }),
});
if (!response.ok) throw new Error(`Valeron returned ${response.status}`);
const data = await response.json();
```

```python title="Python"
import os
import requests

response = requests.post(
    "https://api.valeron.io/v1/threads",
    headers={"Authorization": f"Bearer {os.environ['VALERON_API_KEY']}"},
    json={
        "attachments": [
            {
                "content_type": "example",
                "data": "example",
                "name": "Jordan Rivera",
            },
        ],
        "body": "Thanks for your time today. Here is what we agreed.",
        "contact_id": "ct_01JCH8XFQH",
        "html": "example",
        "line_id": "lin_01JCP6WF0H",
        "mailbox_id": "mbx_01JRJX4977",
    },
)
response.raise_for_status()
data = response.json()
```

### Response 201

```json
{
  "id": "thr_01JWGG21AJ",
  "values": {
    "example_key": {
      "amount": 2400000,
      "currency": "USD"
    }
  }
}
```

### Response 401

```json
{
  "error": null
}
```

### Response 402

```json
{
  "error": null
}
```

### Response 403

```json
{
  "error": null
}
```

### Response 404

```json
{
  "error": null
}
```
