# Email SDKs & MCP

The email API is plain JSON over HTTPS, so any HTTP client works: every page in these docs shows the raw requests. SDKs for Node.js and PHP wrap them, and the OnCharge MCP server lets AI agents send email and read the log.

> **Early access: the SDKs are not published yet.** Package names, method names and options on this page can still change. Until the packages are out, call the API directly as shown in the [quickstart](https://oncharge.io/docs/messaging).

## Node.js

The `emails` resource of `@oncharge/node` (Node 18.17+, ESM and CommonJS, no dependencies), the same package as the [payments SDK](https://oncharge.io/docs/sdks).

```bash
npm install @oncharge/node
```

```ts
import { OnCharge } from '@oncharge/node';

const oncharge = new OnCharge({ apiKey: process.env.ONCHARGE_MESSAGING_KEY! });

// Send one email. The idempotency key makes retries safe.
const { id } = await oncharge.emails.send(
  {
    from: 'Example Store <orders@example.com>',
    to: ['shopper@example.net'],
    subject: 'Your order #1001 is confirmed',
    html: '<p>Thanks for your order.</p>',
    tags: [{ name: 'type', value: 'receipt' }],
  },
  { idempotencyKey: 'order-1001-receipt' },
);

// Up to 1,000 at once.
const { data } = await oncharge.emails.batch([
  { from: 'Example Store <orders@example.com>', to: 'a@example.net', subject: 'Order 1001 shipped', text: 'On its way.' },
  { from: 'Example Store <orders@example.com>', to: 'b@example.net', subject: 'Order 1002 shipped', text: 'On its way.' },
]);

// Read the log.
const email = await oncharge.emails.retrieve(id);
console.log(email.last_event);
```

Errors are thrown with the API's `error.type` and `message`, so you can branch on the type (see [Limits & errors](https://oncharge.io/docs/messaging/limits#error-types)). Webhooks are verified with the SDK's existing helper, as for payment events: [Webhooks: with the Node SDK](https://oncharge.io/docs/webhooks#with-the-node-sdk).

## PHP

```bash
composer require oncharge/oncharge-php
```

```php
<?php
require __DIR__ . '/vendor/autoload.php';

$oncharge = new \OnCharge\OnCharge(getenv('ONCHARGE_MESSAGING_KEY'));

$email = $oncharge->emails->send([
    'from' => 'Example Store <orders@example.com>',
    'to' => ['shopper@example.net'],
    'subject' => 'Your order #1001 is confirmed',
    'html' => '<p>Thanks for your order.</p>',
], ['idempotency_key' => 'order-1001-receipt']);

echo $email['id'];
```

Until the package is published, the cURL example in the [quickstart](https://oncharge.io/docs/messaging#php) does the same with no dependencies.

## Other languages

Use the HTTP API directly. The requests and responses on these pages are complete: a bearer token, a JSON body and a JSON response. An OpenAPI 3.1 description of the email API will be published with the SDKs, so you can generate a client in your language.

## MCP server for AI agents {#mcp}

The OnCharge MCP server, [`@oncharge/mcp`](https://oncharge.io/docs/ai#5-mcp-server-docs-and-live-api-tools), is gaining tools for the email API, so an AI agent or coding assistant can:

- send an email, or a batch, with an idempotency key;
- create, update and publish templates;
- look up emails in the log and read their latest event;
- check a domain's records and status.

Give the agent a key with the `sending` scope, limited to one domain, unless it needs more. The MCP server's docs tools already cover these pages, so an assistant can look up fields and errors before it sends.
