# Add a contact

> POST /v1/contacts

One person to a project's phonebook: their number, the name the console shows, the `attributes` an agent can say about them, and their time zone. A number already there is `409 contact_exists` — to add or update many at once, import them. Needs `contacts:write` and a live key.

## Body

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

| Field | Type | Required | Description |
|---|---|---|---|
| `attributes` | object | No | What an agent may say about them: its `{{placeholders}}` take these by name. Kept as text. |
| `name` | string | No | What the console calls them. Never spoken. |
| `phone` | string | Yes | Their number, in international format. |
| `project_id` | string (uuid) | Yes | The project whose phonebook they join. |
| `timezone` | string or null | No | Where they are, like `Asia/Riyadh`: a campaign's calling hours are read there. Leave it out for your organization's own. |

## Answer

`201` with [Contact](/reference/objects/Contact).

## 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, or a key limited to some agents. |
| `404` | This key reaches no such project. |
| `409` | The number is already in the project's phonebook, or the project is frozen. |
| `422` | Not a phone number, or not a time zone. |
| `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 someone to the phonebook. With a live key: the phonebook holds real people.
curl -sS --fail-with-body -X POST "https://api.aigently.ai/v1/contacts" \
  -H "Authorization: Bearer $AIGENTLY_API_KEY" \
  --json '{
  "project_id": "0b1c2d3e-4f5a-6b7c-8d9e-0f1a2b3c4d5e",
  "phone": "+12025550131",
  "name": "Sara Haddad",
  "attributes": {
    "first_name": "Sara",
    "member_since": "2023"
  },
  "timezone": "America/New_York"
}'
```

**JavaScript**

```js
// Add someone to the phonebook. With a live key: the phonebook holds real people.
const response = await fetch("https://api.aigently.ai/v1/contacts", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.AIGENTLY_API_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "project_id": "0b1c2d3e-4f5a-6b7c-8d9e-0f1a2b3c4d5e",
    "phone": "+12025550131",
    "name": "Sara Haddad",
    "attributes": {
      "first_name": "Sara",
      "member_since": "2023"
    },
    "timezone": "America/New_York"
  }),
});
const answer = await response.json();
if (!response.ok) throw new Error(`${answer.code}: ${answer.detail}`);
console.log(answer);
```

**Python**

```python
# Add someone to the phonebook. With a live key: the phonebook holds real people.
# pip install httpx
import os

import httpx

response = httpx.post(
    "https://api.aigently.ai/v1/contacts",
    headers={
        "Authorization": f"Bearer {os.environ['AIGENTLY_API_KEY']}",
    },
    json={
        "project_id": "0b1c2d3e-4f5a-6b7c-8d9e-0f1a2b3c4d5e",
        "phone": "+12025550131",
        "name": "Sara Haddad",
        "attributes": {
            "first_name": "Sara",
            "member_since": "2023",
        },
        "timezone": "America/New_York",
    },
)
if response.is_error:
    raise SystemExit(response.text)
print(response.json())
```

**Go**

```go
// Add someone to the phonebook. With a live key: the phonebook holds real people.
package main

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

func main() {
	payload := []byte(`{
  "project_id": "0b1c2d3e-4f5a-6b7c-8d9e-0f1a2b3c4d5e",
  "phone": "+12025550131",
  "name": "Sara Haddad",
  "attributes": {
    "first_name": "Sara",
    "member_since": "2023"
  },
  "timezone": "America/New_York"
}`)
	request, err := http.NewRequest("POST", "https://api.aigently.ai/v1/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 someone to the phonebook. With a live key: the phonebook holds real people.
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 = """
            {
              "project_id": "0b1c2d3e-4f5a-6b7c-8d9e-0f1a2b3c4d5e",
              "phone": "+12025550131",
              "name": "Sara Haddad",
              "attributes": {
                "first_name": "Sara",
                "member_since": "2023"
              },
              "timezone": "America/New_York"
            }
            """;
        HttpRequest request = HttpRequest.newBuilder(URI.create("https://api.aigently.ai/v1/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 someone to the phonebook. With a live key: the phonebook holds real people.
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/contacts")
{
    Content = new StringContent(
        """
        {
          "project_id": "0b1c2d3e-4f5a-6b7c-8d9e-0f1a2b3c4d5e",
          "phone": "+12025550131",
          "name": "Sara Haddad",
          "attributes": {
            "first_name": "Sara",
            "member_since": "2023"
          },
          "timezone": "America/New_York"
        }
        """,
        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 someone to the phonebook. With a live key: the phonebook holds real people.
$curl = curl_init("https://api.aigently.ai/v1/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([
        "project_id" => "0b1c2d3e-4f5a-6b7c-8d9e-0f1a2b3c4d5e",
        "phone" => "+12025550131",
        "name" => "Sara Haddad",
        "attributes" => [
            "first_name" => "Sara",
            "member_since" => "2023",
        ],
        "timezone" => "America/New_York",
    ]),
]);
$body = curl_exec($curl);
if (curl_getinfo($curl, CURLINFO_RESPONSE_CODE) >= 400) {
    fwrite(STDERR, $body . "\n");
    exit(1);
}
echo $body, "\n";
```
