# Batch sending

> Send up to 100 emails in a single request with POST /emails/batch.

Send multiple distinct emails in one request with `POST /emails/batch`. The body is a JSON array of email objects — each with the same shape as a single [POST /emails](https://www.mailblastr.com/docs/api/emails-send) body. A batch may contain **up to 100** emails.

## When to use batch sending

Reach for the batch endpoint when you need to send many distinct transactional emails at once — order confirmations, notifications, or per-recipient messages with unique content — and want to cut the number of API calls. For marketing email to an audience, use a [campaign](https://www.mailblastr.com/docs/audiences/overview) instead.

## How a batch is processed

MailBlastr validates **every** email in the batch *before* sending any of them. If a single item fails validation, the whole request is rejected with that item's error and nothing is sent — so an invalid item late in the array can never leave earlier items already delivered. Once all items pass validation, they are sent in order.

> **Note:** Each item is an independent email with its own `from`, `to`, `subject`, body, headers, and tags. Suppressed recipients are skipped per-item, exactly as for a single send.

## Limitations

- A batch holds a maximum of **100 emails**; an empty array is also rejected.
- The `attachments` field is **not supported** in a batch — send those as single [POST /emails](https://www.mailblastr.com/docs/api/emails-send) requests. See [Attachments](https://www.mailblastr.com/docs/emails/attachments).
- The `scheduled_at` field is **not supported** in a batch — schedule those as single sends. See [Schedule email](https://www.mailblastr.com/docs/emails/schedule).
- A single invalid item (missing required field, invalid data) fails the whole request and nothing is sent.

> **Note:** You can make a batch send safe to retry by passing an `Idempotency-Key` header for the whole batch — see [Idempotency keys](https://www.mailblastr.com/docs/emails/idempotency). Choose a key that represents the batch (e.g. a team or job id).

## Send a batch

**Node.js**

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

const mb = new MailBlastr('mb_xxxxxxxxx');

const { data, error } = await mb.batch.send([
  {
    "from": "Acme <hello@yourdomain.com>",
    "to": ["first@example.com"],
    "subject": "Welcome, first user",
    "html": "<p>Hello!</p>"
  },
  {
    "from": "Acme <hello@yourdomain.com>",
    "to": ["second@example.com"],
    "subject": "Welcome, second user",
    "html": "<p>Hello!</p>"
  }
]);
console.log({ data, error });
```

**Ruby**

```ruby
require "mailblastr"

Mailblastr.api_key = "mb_xxxxxxxxx"

Mailblastr::Batch.send([
  {
    "from": "Acme <hello@yourdomain.com>",
    "to": [
      "first@example.com"
    ],
    "subject": "Welcome, first user",
    "html": "<p>Hello!</p>"
  },
  {
    "from": "Acme <hello@yourdomain.com>",
    "to": [
      "second@example.com"
    ],
    "subject": "Welcome, second user",
    "html": "<p>Hello!</p>"
  }
])
```

**PHP**

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

$mailblastr->batch->send([
  [
    'from' => "Acme <hello@yourdomain.com>",
    'to' => [
      "first@example.com"
    ],
    'subject' => "Welcome, first user",
    'html' => "<p>Hello!</p>"
  ],
  [
    'from' => "Acme <hello@yourdomain.com>",
    'to' => [
      "second@example.com"
    ],
    'subject' => "Welcome, second user",
    'html' => "<p>Hello!</p>"
  ]
]);
```

**Python**

```python
import mailblastr

mailblastr.api_key = "mb_xxxxxxxxx"

mailblastr.Batch.send([
  {
    "from": "Acme <hello@yourdomain.com>",
    "to": [
      "first@example.com"
    ],
    "subject": "Welcome, first user",
    "html": "<p>Hello!</p>"
  },
  {
    "from": "Acme <hello@yourdomain.com>",
    "to": [
      "second@example.com"
    ],
    "subject": "Welcome, second user",
    "html": "<p>Hello!</p>"
  }
])
```

**Go**

```go
package main

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

func main() {
    req, _ := http.NewRequest("POST", "https://api.mailblastr.com/emails/batch", strings.NewReader(`[
  {
    "from": "Acme <hello@yourdomain.com>",
    "to": ["first@example.com"],
    "subject": "Welcome, first user",
    "html": "<p>Hello!</p>"
  },
  {
    "from": "Acme <hello@yourdomain.com>",
    "to": ["second@example.com"],
    "subject": "Welcome, second user",
    "html": "<p>Hello!</p>"
  }
]`))
    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/emails/batch")
        .header("Authorization", "Bearer mb_xxxxxxxxx")
        .header("Content-Type", "application/json")
        .body(r#"[
  {
    "from": "Acme <hello@yourdomain.com>",
    "to": ["first@example.com"],
    "subject": "Welcome, first user",
    "html": "<p>Hello!</p>"
  },
  {
    "from": "Acme <hello@yourdomain.com>",
    "to": ["second@example.com"],
    "subject": "Welcome, second user",
    "html": "<p>Hello!</p>"
  }
]"#)
        .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/emails/batch"))
    .header("Authorization", "Bearer mb_xxxxxxxxx")
    .header("Content-Type", "application/json")
    .method("POST", HttpRequest.BodyPublishers.ofString("""
[
  {
    "from": "Acme <hello@yourdomain.com>",
    "to": ["first@example.com"],
    "subject": "Welcome, first user",
    "html": "<p>Hello!</p>"
  },
  {
    "from": "Acme <hello@yourdomain.com>",
    "to": ["second@example.com"],
    "subject": "Welcome, second user",
    "html": "<p>Hello!</p>"
  }
]
"""))
    .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/emails/batch");
request.Headers.Add("Authorization", "Bearer mb_xxxxxxxxx");
request.Content = new StringContent(@"[
  {
    ""from"": ""Acme <hello@yourdomain.com>"",
    ""to"": [""first@example.com""],
    ""subject"": ""Welcome, first user"",
    ""html"": ""<p>Hello!</p>""
  },
  {
    ""from"": ""Acme <hello@yourdomain.com>"",
    ""to"": [""second@example.com""],
    ""subject"": ""Welcome, second user"",
    ""html"": ""<p>Hello!</p>""
  }
]", 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/emails/batch' \
  -H 'Authorization: Bearer mb_xxxxxxxxx' \
  -H 'Content-Type: application/json' \
  -d '[
  {
    "from": "Acme <hello@yourdomain.com>",
    "to": ["first@example.com"],
    "subject": "Welcome, first user",
    "html": "<p>Hello!</p>"
  },
  {
    "from": "Acme <hello@yourdomain.com>",
    "to": ["second@example.com"],
    "subject": "Welcome, second user",
    "html": "<p>Hello!</p>"
  }
]'
```

**CLI**

```bash
mailblastr emails batch \
  --data '[{"from":"Acme <hello@yourdomain.com>","to":["first@example.com"],"subject":"Welcome, first user","html":"<p>Hello!</p>"},{"from":"Acme <hello@yourdomain.com>","to":["second@example.com"],"subject":"Welcome, second user","html":"<p>Hello!</p>"}]'
```

## Response

The response is a `data` array of the created emails, each with its `id`, in the same order as the request.

```json
{
  "data": [
    { "id": "49a3999c-0ce1-4ea6-ab68-afcd6dc2e794" },
    { "id": "1b2c3d4e-5f60-7a8b-9c0d-1e2f3a4b5c6d" }
  ]
}
```

> **Warning:** A batch larger than 100 emails, or an empty array, is rejected with a `validation_error`.
