# Add contacts to a campaign

> POST /v1/campaigns/{campaign_id}/contacts

Up to 1,000 more people, to a campaign that is a draft, running or paused. A running campaign starts calling them in its next calling hours. A number the campaign already holds is skipped and listed in `already_in_campaign`, so sending a batch again is safe. Needs `campaigns:write` and a live key.

## Parameters

| Name | In | Type | Required | Description |
|---|---|---|---|---|
| `campaign_id` | path | string (uuid) | Yes |  |

## Body

Sent as JSON. A field this operation does not take is refused, so a typo fails loudly.

| Field | Type | Required | Description |
|---|---|---|---|
| `contacts` | array of [CampaignContactCreate](/reference/objects/CampaignContactCreate) | Yes | The people to add, up to 1,000 in one request. |

## Answer

`200` with [CampaignContactsAdded](/reference/objects/CampaignContactsAdded).

## Errors

Every error is a [problem document](/errors) with a stable `code`.

| Status | When |
|---|---|
| `401` | The key is missing, mistyped, unknown, revoked or expired. |
| `403` | A test key: a campaign rings real people. |
| `404` | This key reaches no such campaign. |
| `409` | The campaign has finished: create a new one for a new list. |
| `422` | A contact does not fit — `errors` names each by its place in `contacts`. |
| `429` | Too many requests for this key or its organization; see `Retry-After`. |
| `500` | Something went wrong on our side. Quote the `request_id` to support. |

## Examples

**curl**

```sh
#!/bin/sh
# Add people to a campaign. A number the campaign already holds is skipped, so sending a batch again is safe.
curl -sS --fail-with-body -X POST "https://api.aigently.ai/v1/campaigns/1c3e5a7b-9d0f-4b2c-8e4a-6c8e0a2c4e6b/contacts" \
  -H "Authorization: Bearer $AIGENTLY_API_KEY" \
  --json '{
  "contacts": [
    {
      "to_number": "+12025550125",
      "variables": {
        "first_name": "Lina"
      },
      "external_id": "lead-20933"
    }
  ]
}'
```

**JavaScript**

```js
// Add people to a campaign. A number the campaign already holds is skipped, so sending a batch again is safe.
const response = await fetch("https://api.aigently.ai/v1/campaigns/1c3e5a7b-9d0f-4b2c-8e4a-6c8e0a2c4e6b/contacts", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.AIGENTLY_API_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "contacts": [
      {
        "to_number": "+12025550125",
        "variables": {
          "first_name": "Lina"
        },
        "external_id": "lead-20933"
      }
    ]
  }),
});
const answer = await response.json();
if (!response.ok) throw new Error(`${answer.code}: ${answer.detail}`);
console.log(answer);
```

**Python**

```python
# Add people to a campaign. A number the campaign already holds is skipped, so sending a batch again is safe.
# pip install httpx
import os

import httpx

response = httpx.post(
    "https://api.aigently.ai/v1/campaigns/1c3e5a7b-9d0f-4b2c-8e4a-6c8e0a2c4e6b/contacts",
    headers={
        "Authorization": f"Bearer {os.environ['AIGENTLY_API_KEY']}",
    },
    json={
        "contacts": [{"to_number": "+12025550125", "variables": {"first_name": "Lina"}, "external_id": "lead-20933"}],
    },
)
if response.is_error:
    raise SystemExit(response.text)
print(response.json())
```

**Go**

```go
// Add people to a campaign. A number the campaign already holds is skipped, so sending a batch again is safe.
package main

import (
	"bytes"
	"fmt"
	"io"
	"net/http"
	"os"
)

func main() {
	payload := []byte(`{
  "contacts": [
    {
      "to_number": "+12025550125",
      "variables": {
        "first_name": "Lina"
      },
      "external_id": "lead-20933"
    }
  ]
}`)
	request, err := http.NewRequest("POST", "https://api.aigently.ai/v1/campaigns/1c3e5a7b-9d0f-4b2c-8e4a-6c8e0a2c4e6b/contacts", bytes.NewReader(payload))
	if err != nil {
		panic(err)
	}
	request.Header.Set("Authorization", "Bearer "+os.Getenv("AIGENTLY_API_KEY"))
	request.Header.Set("Content-Type", "application/json")
	response, err := http.DefaultClient.Do(request)
	if err != nil {
		panic(err)
	}
	defer response.Body.Close()
	body, _ := io.ReadAll(response.Body)
	if response.StatusCode >= 400 {
		fmt.Fprintln(os.Stderr, string(body))
		os.Exit(1)
	}
	fmt.Println(string(body))
}
```

**Java**

```java
// Add people to a campaign. A number the campaign already holds is skipped, so sending a batch again is safe.
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class Main {
    public static void main(String[] args) throws Exception {
        String body = """
            {
              "contacts": [
                {
                  "to_number": "+12025550125",
                  "variables": {
                    "first_name": "Lina"
                  },
                  "external_id": "lead-20933"
                }
              ]
            }
            """;
        HttpRequest request = HttpRequest.newBuilder(URI.create("https://api.aigently.ai/v1/campaigns/1c3e5a7b-9d0f-4b2c-8e4a-6c8e0a2c4e6b/contacts"))
            .header("Authorization", "Bearer " + System.getenv("AIGENTLY_API_KEY"))
            .header("Content-Type", "application/json")
            .method("POST", HttpRequest.BodyPublishers.ofString(body))
            .build();
        // HTTP/1.1: on a plain-http address Java's default asks to upgrade, which not every
        // server allows.
        HttpClient client = HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1).build();
        HttpResponse<String> response =
            client.send(request, HttpResponse.BodyHandlers.ofString());
        if (response.statusCode() >= 400) {
            System.err.println(response.body());
            System.exit(1);
        }
        System.out.println(response.body());
    }
}
```

**C#**

```csharp
// Add people to a campaign. A number the campaign already holds is skipped, so sending a batch again is safe.
using System.Net.Http.Headers;
using System.Text;

using var client = new HttpClient();
client.DefaultRequestHeaders.Authorization =
    new AuthenticationHeaderValue("Bearer", Environment.GetEnvironmentVariable("AIGENTLY_API_KEY"));
var request = new HttpRequestMessage(HttpMethod.Post, "https://api.aigently.ai/v1/campaigns/1c3e5a7b-9d0f-4b2c-8e4a-6c8e0a2c4e6b/contacts")
{
    Content = new StringContent(
        """
        {
          "contacts": [
            {
              "to_number": "+12025550125",
              "variables": {
                "first_name": "Lina"
              },
              "external_id": "lead-20933"
            }
          ]
        }
        """,
        Encoding.UTF8,
        "application/json"),
};
var response = await client.SendAsync(request);
var body = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
{
    Console.Error.WriteLine(body);
    return 1;
}
Console.WriteLine(body);
return 0;
```

**PHP**

```php
<?php
// Add people to a campaign. A number the campaign already holds is skipped, so sending a batch again is safe.
$curl = curl_init("https://api.aigently.ai/v1/campaigns/1c3e5a7b-9d0f-4b2c-8e4a-6c8e0a2c4e6b/contacts");
curl_setopt_array($curl, [
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        "Authorization: Bearer " . getenv("AIGENTLY_API_KEY"),
        "Content-Type: application/json",
    ],
    CURLOPT_POSTFIELDS => json_encode([
        "contacts" => [
            [
                "to_number" => "+12025550125",
                "variables" => [
                    "first_name" => "Lina",
                ],
                "external_id" => "lead-20933",
            ],
        ],
    ]),
]);
$body = curl_exec($curl);
if (curl_getinfo($curl, CURLINFO_RESPONSE_CODE) >= 400) {
    fwrite(STDERR, $body . "\n");
    exit(1);
}
echo $body, "\n";
```
