# Import contacts in bulk (/docs/api-reference/contacts/import-contacts)

Creates or updates up to 1000 contacts at once (source `import`). Each entry is validated on its own: invalid ones are skipped and listed in `invalid`. Existing contacts get the new names and properties, but an import never re-subscribes an unsubscribed contact (`kept_unsubscribed`). Imports send no `contact.*` webhooks. Over the plan's contact limit nothing is imported.

`POST https://api.coss.com/v1/contacts/import`

Authenticate with your API key as a bearer token (`Authorization: Bearer coss_test_…`). Responses are JSON with snake_case fields; errors are `{ error: { type, code, message, param?, issues?, request_id } }` (https://coss.com/docs/api-reference/errors.md).

## Request body

- `contacts` (object[], required, 1-1000 items): The contacts to create or update, at most 1,000.
  - `email` (string, required): The contact's address (validated on its own).
  - `first_name` (string | null): Their first name.
  - `last_name` (string | null): Their last name.
  - `properties` (map of string | number | boolean | null): Custom properties by key, checked against their definitions.
  - `status` (string): `subscribed` or `unsubscribed` (anything else, like an export's `pending`, is reported in `invalid`). Omitted, a new contact is `subscribed` and an existing one keeps its status.
- `audience_id` (string): Adds every imported contact to this audience.

## Example request

TypeScript (the `coss` package):

```ts
import { Coss } from "coss";

const coss = new Coss({
  apiKey: process.env.COSS_API_KEY,
  baseUrl: "https://api.coss.com",
});

const { data, error } = await coss.email.contacts.import({
  contacts: [
    {
      email: "ada@example.com",
      first_name: "Ada",
      properties: {
        plan: "pro",
      },
    },
    {
      email: "grace@example.com",
      status: "unsubscribed",
    },
  ],
});
```

HTTP:

```sh
curl -X POST "https://api.coss.com/v1/contacts/import" \
  -H "Authorization: Bearer $COSS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "contacts": [
    {
      "email": "ada@example.com",
      "first_name": "Ada",
      "properties": {
        "plan": "pro"
      }
    },
    {
      "email": "grace@example.com",
      "status": "unsubscribed"
    }
  ]
}'
```

Python:

```python
from coss import Coss
from coss.email.contacts import ImportContactsRequestContactsItem

client = Coss()  # api_key= or COSS_API_KEY

client.email.contacts.import_(
    contacts=[
        ImportContactsRequestContactsItem(
            email="ada@example.com",
            first_name="Ada",
            properties={
                "plan": "pro"
            },
        ),
        ImportContactsRequestContactsItem(
            email="grace@example.com",
            status="unsubscribed",
        )
    ],
)
```

Go:

```go
import (
	"context"

	coss "github.com/cosscom/coss-go"
	cossclient "github.com/cosscom/coss-go/client"
	email "github.com/cosscom/coss-go/email"
)

client := cossclient.NewClient() // option.WithAPIKey(…) or COSS_API_KEY

request := &email.ImportContactsRequest{
    Contacts: []*email.ImportContactsRequestContactsItem{
        &email.ImportContactsRequestContactsItem{
            Email: "ada@example.com",
            FirstName: coss.String(
                "Ada",
            ),
            Properties: map[string]*email.ImportContactsRequestContactsItemPropertiesValue{
                "plan": &email.ImportContactsRequestContactsItemPropertiesValue{
                    String: "pro",
                },
            },
        },
        &email.ImportContactsRequestContactsItem{
            Email: "grace@example.com",
            Status: coss.String(
                "unsubscribed",
            ),
        },
    },
}
client.Email.Contacts.Import(
    context.TODO(),
    request,
)
```

Ruby:

```ruby
require "coss"

client = Coss::Client.new # api_key: or COSS_API_KEY

client.email.contacts.import(contacts: [{
  email: "ada@example.com",
  first_name: "Ada",
  properties: {
    plan: "pro"
  }
}, {
  email: "grace@example.com",
  status: "unsubscribed"
}])
```

PHP:

```php
<?php

use Coss\Coss;
use Coss\Email\Contacts\Requests\ImportContactsRequest;
use Coss\Email\Contacts\Types\ImportContactsRequestContactsItem;

$client = new Coss(); // apiKey: or COSS_API_KEY

$client->email->contacts->import(
    new ImportContactsRequest([
        'contacts' => [
            new ImportContactsRequestContactsItem([
                'email' => 'ada@example.com',
                'firstName' => 'Ada',
                'properties' => [
                    'plan' => 'pro',
                ],
            ]),
            new ImportContactsRequestContactsItem([
                'email' => 'grace@example.com',
                'status' => 'unsubscribed',
            ]),
        ],
    ]),
);
```

Java:

```java
import com.coss.Coss;
import com.coss.resources.email.contacts.requests.ImportContactsRequest;
import com.coss.resources.email.contacts.types.ImportContactsRequestContactsItem;
import com.coss.resources.email.contacts.types.ImportContactsRequestContactsItemPropertiesValue;
import java.util.Arrays;
import java.util.HashMap;

Coss client = Coss.builder().build(); // .apiKey(…) or COSS_API_KEY

client.email().contacts().import_(
    ImportContactsRequest
        .builder()
        .contacts(
            Arrays.asList(
                ImportContactsRequestContactsItem
                    .builder()
                    .email("ada@example.com")
                    .firstName(
                        OptionalNullable.of("Ada")
                    )
                    .properties(
                        new HashMap<String, OptionalNullable<ImportContactsRequestContactsItemPropertiesValue>>() {{
                            put("plan", OptionalNullable.of(
                                ImportContactsRequestContactsItemPropertiesValue.of("pro")
                            ));
                        }}
                    )
                    .build(),
                ImportContactsRequestContactsItem
                    .builder()
                    .email("grace@example.com")
                    .status("unsubscribed")
                    .build()
            )
        )
        .build()
);
```

.NET:

```csharp
using Coss;
using Coss.Email;

var client = new CossClient(); // apiKey: or COSS_API_KEY

await client.Email.Contacts.ImportAsync(
    new ImportContactsRequest
    {
        Contacts = new List<ImportContactsRequestContactsItem>()
        {
            new ImportContactsRequestContactsItem
            {
                Email = "ada@example.com",
                FirstName = "Ada",
                Properties = new Dictionary<string, OneOf<string, double, bool>?>()
                {
                    { "plan", "pro" },
                },
            },
            new ImportContactsRequestContactsItem
            {
                Email = "grace@example.com",
                Status = "unsubscribed",
            },
        },
    }
);
```

## Response 200

What the import did.

- `object` ("contact_import", required)
- `created` (integer, required): Contacts created.
- `updated` (integer, required): Existing contacts updated.
- `kept_unsubscribed` (integer, required): Existing contacts the import marked `subscribed` that stay `unsubscribed`: an import never re-subscribes anyone.
- `invalid` (object[], required): Entries that were refused. The rest of the import still went through.
  - `index` (integer, required): The entry's position in `contacts`, from 0.
  - `email` (string, required): The address it gave.
  - `message` (string, required): Why it was refused.

Example:

```json
{
  "object": "contact_import",
  "created": 1,
  "updated": 1,
  "kept_unsubscribed": 0,
  "invalid": [
    {
      "index": 2,
      "email": "not-an-address",
      "message": "Must be an email address."
    }
  ]
}
```

## Errors

- `400`: The body is not valid JSON (`invalid_json`). Also when the `Coss-Version` header names an unknown API version (`invalid_api_version`).
- `401`: The API key is missing, invalid, revoked or expired (`authentication_error`).
- `402`: The import would exceed the organization's plan's contact limit (`plan_limit_reached`) (`billing_error`). Never retried: it needs a plan change or a payment.
- `403`: The API key lacks the permission this endpoint needs (`permission_error`), or its organization is suspended (`organization_suspended`).
- `413`: The request body is larger than 5 MB (`payload_too_large`).
- `415`: The body is not sent as `application/json` (`unsupported_media_type`).
- `422`: The body is not a list of 1-1000 contacts (`validation_error`), or `audience_id` is unknown (`audience_not_found`).
- `429`: Too many requests with this API key (`rate_limited`), or too many requests with invalid keys from this address (`too_many_failed_authentications`). Retry after `Retry-After` seconds. Successful responses carry `RateLimit-Limit`, `RateLimit-Remaining` and `RateLimit-Reset` (seconds until the limit is fully restored).
- `500`: Unexpected server error (`api_error` / `internal_error`).
- `503`: The API key could not be verified right now; retry (`service_unavailable`).