# Create contact

> POST /contacts — add a contact to a sending domain (upsert by email).

`POST /contacts`

Add a contact to one of your **sending domains**. Contacts are domain-scoped: pass the `domain` the contact belongs to along with the required `email`. If a contact with that email already exists in the domain, it is **updated** instead of duplicated (upsert by email). Passing `audience_id` on this endpoint is rejected with a `422` — use the [audience-scoped variant](#audience-scoped-variant) below when you want to target an audience explicitly.

**Body**

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `domain` | string | Yes | The sending domain the contact belongs to (one of your domains, e.g. `yourdomain.com`). |
| `email` | string | Yes | The contact’s email address. Stored lowercased. Must be a valid email. |
| `first_name` | string | No | Optional first name. Stored as null when omitted. |
| `last_name` | string | No | Optional last name. Stored as null when omitted. |
| `unsubscribed` | boolean | No | Optional opt-out flag. Defaults to false. When true, campaigns skip this contact. |
| `properties` | object | No | Optional map of custom property keys and values to attach to the contact (e.g. `{ "company_name": "Acme Corp", "score": 10 }`). Each key must be a registered contact property and the value must match its declared type (`string` or `number`). |

**Node.js**

```js
import { MailBlastr } from 'mailblastr';

const mb = new MailBlastr('mb_xxxxxxxxx');

const { data, error } = await mb.contacts.create({
  "domain": "yourdomain.com",
  "email": "steve@example.com",
  "first_name": "Steve",
  "last_name": "Wozniak",
  "unsubscribed": false
});
console.log({ data, error });
```

**Ruby**

```ruby
require "mailblastr"

Mailblastr.api_key = "mb_xxxxxxxxx"

Mailblastr::Contacts.create({
  "domain": "yourdomain.com",
  "email": "steve@example.com",
  "first_name": "Steve",
  "last_name": "Wozniak",
  "unsubscribed": false
})
```

**PHP**

```php
$mailblastr = Mailblastr::client('mb_xxxxxxxxx');

$mailblastr->contacts->create([
  'domain' => "yourdomain.com",
  'email' => "steve@example.com",
  'first_name' => "Steve",
  'last_name' => "Wozniak",
  'unsubscribed' => false
]);
```

**Python**

```python
import mailblastr

mailblastr.api_key = "mb_xxxxxxxxx"

mailblastr.Contacts.create({
  "domain": "yourdomain.com",
  "email": "steve@example.com",
  "first_name": "Steve",
  "last_name": "Wozniak",
  "unsubscribed": False
})
```

**Go**

```go
client := mailblastr.NewClient("mb_xxxxxxxxx")

params := &mailblastr.CreateContactRequest{
    Domain:    "yourdomain.com",
    Email:     "jane@example.com",
    FirstName: "Jane",
}
contact, err := client.Contacts.Create(params)
```

**Rust**

```rust
let mb = Mailblastr::new("mb_xxxxxxxxx");

let contact = CreateContactOptions::new("jane@example.com")
    .with_domain("yourdomain.com")
    .with_first_name("Jane");
let _created = mb.contacts.create(contact).await?;
```

**Java**

```java
Mailblastr mailblastr = new Mailblastr("mb_xxxxxxxxx");

CreateContactRequest request = CreateContactRequest.builder()
    .domain("yourdomain.com")
    .email("jane@example.com")
    .firstName("Jane")
    .build();
mailblastr.contacts().create(request);
```

**.NET**

```csharp
IMailblastr mailblastr = MailblastrClient.Create("mb_xxxxxxxxx");

var resp = await mailblastr.ContactCreateAsync(new ContactCreateOptions
{
    Domain = "yourdomain.com",
    Email = "jane@example.com",
    FirstName = "Jane",
});
```

**cURL**

```bash
curl -X POST 'https://api.mailblastr.com/contacts' \
  -H 'Authorization: Bearer mb_xxxxxxxxx' \
  -H 'Content-Type: application/json' \
  -d '{
  "domain": "yourdomain.com",
  "email": "steve@example.com",
  "first_name": "Steve",
  "last_name": "Wozniak",
  "unsubscribed": false
}'
```

**CLI**

```bash
mailblastr contacts create \
  --domain 'yourdomain.com' \
  --email 'steve@example.com' \
  --first-name 'Steve' \
  --last-name 'Wozniak'
```

**Response (201)**

```json
{
  "object": "contact",
  "id": "479e3145-dd0e-4f64-bf48-1d4b6d4cd8f6"
}
```

> **Note:** Upsert: re-posting an existing email merges the incoming `first_name`, `last_name`, and `properties` into the existing contact (preserving existing name values when omitted) and applies `unsubscribed` monotonically (once unsubscribed, a re-post cannot resubscribe). Use [PATCH](https://www.mailblastr.com/docs/api/contacts-update) to change only some fields.

## Audience-scoped variant

The nested route `POST /audiences/:audience_id/contacts` still works and adds the contact to a specific audience instead of a domain — no `domain` field in the body:

**Node.js**

```js
import { MailBlastr } from 'mailblastr';

const mb = new MailBlastr('mb_xxxxxxxxx');

const { data, error } = await mb.contacts.create({
  audienceId: 'AUDIENCE_ID',
  "email": "steve@example.com",
  "first_name": "Steve",
  "last_name": "Wozniak",
  "unsubscribed": false
});
console.log({ data, error });
```

**Ruby**

```ruby
require 'net/http'
require 'uri'

uri = URI('https://api.mailblastr.com/audiences/AUDIENCE_ID/contacts')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
req = Net::HTTP::Post.new(uri)
req['Authorization'] = 'Bearer mb_xxxxxxxxx'
req['Content-Type'] = 'application/json'
req.body = <<~JSON
{
  "email": "steve@example.com",
  "first_name": "Steve",
  "last_name": "Wozniak",
  "unsubscribed": false
}
JSON
res = http.request(req)
puts res.body
```

**PHP**

```php
<?php
$ch = curl_init('https://api.mailblastr.com/audiences/AUDIENCE_ID/contacts');
curl_setopt_array($ch, [
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer mb_xxxxxxxxx',
        'Content-Type: application/json',
    ],
    CURLOPT_POSTFIELDS => <<<'JSON'
{
  "email": "steve@example.com",
  "first_name": "Steve",
  "last_name": "Wozniak",
  "unsubscribed": false
}
JSON,
]);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
```

**Python**

```python
import requests

res = requests.post(
    "https://api.mailblastr.com/audiences/AUDIENCE_ID/contacts",
    headers={"Authorization": "Bearer mb_xxxxxxxxx", "Content-Type": "application/json"},
    json={
  "email": "steve@example.com",
  "first_name": "Steve",
  "last_name": "Wozniak",
  "unsubscribed": false
},
)
print(res.json())
```

**Go**

```go
package main

import (
    "fmt"
    "io"
    "net/http"
    "strings"
)

func main() {
    req, _ := http.NewRequest("POST", "https://api.mailblastr.com/audiences/AUDIENCE_ID/contacts", strings.NewReader(`{
  "email": "steve@example.com",
  "first_name": "Steve",
  "last_name": "Wozniak",
  "unsubscribed": false
}`))
    req.Header.Set("Authorization", "Bearer mb_xxxxxxxxx")
    req.Header.Set("Content-Type", "application/json")
    res, _ := http.DefaultClient.Do(req)
    defer res.Body.Close()
    out, _ := io.ReadAll(res.Body)
    fmt.Println(string(out))
}
```

**Rust**

```rust
use reqwest::Client;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let res = Client::new()
        .post("https://api.mailblastr.com/audiences/AUDIENCE_ID/contacts")
        .header("Authorization", "Bearer mb_xxxxxxxxx")
        .header("Content-Type", "application/json")
        .body(r#"{
  "email": "steve@example.com",
  "first_name": "Steve",
  "last_name": "Wozniak",
  "unsubscribed": false
}"#)
        .send()
        .await?;
    println!("{}", res.text().await?);
    Ok(())
}
```

**Java**

```java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

var client = HttpClient.newHttpClient();
var request = HttpRequest.newBuilder()
    .uri(URI.create("https://api.mailblastr.com/audiences/AUDIENCE_ID/contacts"))
    .header("Authorization", "Bearer mb_xxxxxxxxx")
    .header("Content-Type", "application/json")
    .method("POST", HttpRequest.BodyPublishers.ofString("""
{
  "email": "steve@example.com",
  "first_name": "Steve",
  "last_name": "Wozniak",
  "unsubscribed": false
}
"""))
    .build();
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
```

**.NET**

```csharp
using System.Net.Http;
using System.Text;

var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "https://api.mailblastr.com/audiences/AUDIENCE_ID/contacts");
request.Headers.Add("Authorization", "Bearer mb_xxxxxxxxx");
request.Content = new StringContent(@"{
  ""email"": ""steve@example.com"",
  ""first_name"": ""Steve"",
  ""last_name"": ""Wozniak"",
  ""unsubscribed"": false
}", Encoding.UTF8, "application/json");
var response = await client.SendAsync(request);
Console.WriteLine(await response.Content.ReadAsStringAsync());
```

**cURL**

```bash
curl -X POST 'https://api.mailblastr.com/audiences/AUDIENCE_ID/contacts' \
  -H 'Authorization: Bearer mb_xxxxxxxxx' \
  -H 'Content-Type: application/json' \
  -d '{
  "email": "steve@example.com",
  "first_name": "Steve",
  "last_name": "Wozniak",
  "unsubscribed": false
}'
```

A missing `email` or `domain` returns `422 missing_required_field`; an invalid email returns `422 validation_error`. A domain (or audience) that is not yours returns `404 not_found`. See [Errors](https://www.mailblastr.com/docs/api/errors).
