# SDKs

> Die offiziellen Sendtide SDKs für Node.js, Python und PHP: Installation, Optionen, Wiederholungen, Idempotenz, Webhook-Prüfung, Fehler und Pagination.

URL: https://sendtide.ch/docs/sdks/

Die offiziellen SDKs für Node.js, Python und PHP haben keine Abhängigkeiten, wiederholen Fehler richtig, senden Idempotency-Keys von selbst und prüfen Webhook-Signaturen.
| | Node.js | Python | PHP |
| --- | --- | --- | --- |
| Installation | npm install sendtide | pip install sendtide | composer require sendtide/sendtide-php |
| Voraussetzung | Node 18 oder neuer, ESM mit TypeScript-Typen | Python 3.9 oder neuer, nur Standardbibliothek, vollständig typisiert | PHP 8.1 oder neuer mit ext-curl und ext-json |
| Client | new Sendtide({ apiKey }) | Sendtide(api_key) | new Client($apiKey) |

Alle drei lesen den Key sonst aus der Umgebungsvariable SENDTIDE_API_KEY. Für Laravel, Symfony Mailer, Django und Next.js gibt es [Framework-Pakete](https://sendtide.ch/docs/framework-pakete/), für die Kommandozeile die [CLI](https://sendtide.ch/docs/cli/). In einer anderen Sprache nimmst du SMTP oder die REST-API direkt, siehe [Java und.NET](https://sendtide.ch/docs/java-und-net/); die [OpenAPI-Spezifikation](https://api.sendtide.ch/v1/openapi.json) lässt sich in jeden Generator laden.
## Senden

```
import { Sendtide } from "sendtide";

const sendtide = new Sendtide({ apiKey: process.env.SENDTIDE_API_KEY });

const { id, status } = await sendtide.emails.send({
  from: "Acme <hello@example.ch>",
  to: "anna@example.com",
  template: "welcome",
  locale: "de-CH",
  data: { name: "Anna" },
});
```

```
from sendtide import Sendtide

client = Sendtide()  # liest SENDTIDE_API_KEY

res = client.emails.send({
    "from": "Acme <hello@example.ch>",
    "to": "anna@example.com",
    "template": "welcome",
    "locale": "de-CH",
    "data": {"name": "Anna"},
})
print(res["id"], res["status"])
```

```
<?php
use Sendtide\Client;

$sendtide = new Client(getenv('SENDTIDE_API_KEY'));

$res = $sendtide->emails->send([
    'from' => 'Acme <hello@example.ch>',
    'to' => 'anna@example.com',
    'template' => 'welcome',
    'locale' => 'de-CH',
    'data' => ['name' => 'Anna'],
]);
```
## Optionen
| Option | Node.js | Python | PHP |
| --- | --- | --- | --- |
| Basis-URL | baseUrl | base_url | baseUrl |
| Zeitlimit pro Versuch | timeout (ms, Standard 30’000) | timeout (s, Standard 30) | timeout (s, Standard 30) |
| Wiederholungen | maxRetries (2) | max_retries (2) | maxRetries (2) |
| API-Version | apiVersion | api_version | apiVersion |
## Wiederholungen und Idempotenz

Aufrufe, die E-Mails erzeugen (send, batch, sendMime), senden immer einen Idempotency-Key: einen zufälligen UUID, oder deinen eigenen, den du pro Aufruf mitgibst. Bei Wiederholungen bleibt der Key gleich. Wiederholt werden Netzwerkfehler, 429 (mit Retry-After) und 5xx, mit exponentiellem Backoff und Jitter. Ein POST ohne Idempotency-Key wird nur bei 429 wiederholt.

```
await sendtide.emails.send(params, { idempotencyKey: "order-42" });
```

```
client.emails.send(params, idempotency_key="order-42")
```

```
$sendtide->emails->send($params, idempotencyKey: 'order-42');
```
## Batch, Anhänge, Planung

```
import { attachmentFromFile } from "sendtide";

await sendtide.emails.batch({
  defaults: { from: "hello@example.ch", template: "invoice", locale: "de-CH" },
  emails: [
    { to: "anna@example.com", data: { amount: "120.00" } },
    { to: "beat@example.com", data: { amount: "80.00" } },
  ],
});

await sendtide.emails.send({
  from: "hello@example.ch",
  to: "anna@example.com",
  subject: "Deine Rechnung",
  html: "<p>Hallo</p>",
  attachments: [await attachmentFromFile("./rechnung.pdf")],
  send_at: "2026-12-01T08:00:00Z",
});
await sendtide.emails.cancel(id); // solange die E-Mail queued ist
```

```
from sendtide import attachment_from_path

client.emails.batch(
    [
        {"to": "anna@example.com", "data": {"amount": "120.00"}},
        {"to": "beat@example.com", "data": {"amount": "80.00"}},
    ],
    defaults={"from": "hello@example.ch", "template": "invoice", "locale": "de-CH"},
)

res = client.emails.send({
    "from": "hello@example.ch",
    "to": "anna@example.com",
    "subject": "Deine Rechnung",
    "html": "<p>Hallo</p>",
    "attachments": [attachment_from_path("rechnung.pdf")],
    "send_at": "2026-12-01T08:00:00Z",
})
client.emails.cancel(res["id"])  # solange die E-Mail queued ist
```

```
use Sendtide\Attachment;

$sendtide->emails->batch(
    [
        ['to' => 'anna@example.com', 'data' => ['amount' => '120.00']],
        ['to' => 'beat@example.com', 'data' => ['amount' => '80.00']],
    ],
    defaults: ['from' => 'hello@example.ch', 'template' => 'invoice', 'locale' => 'de-CH'],
);

$res = $sendtide->emails->send([
    'from' => 'hello@example.ch',
    'to' => 'anna@example.com',
    'subject' => 'Deine Rechnung',
    'html' => '<p>Hallo</p>',
    'attachments' => [Attachment::fromPath('/tmp/rechnung.pdf')],
    'send_at' => '2026-12-01T08:00:00Z',
]);
$sendtide->emails->cancel($res['id']); // solange die E-Mail queued ist
```

Die Grenzen für Anhänge stehen unter [E-Mails senden](https://sendtide.ch/docs/e-mails-senden/#anh%C3%A4nge).
## Webhooks prüfen

Verwende immer den rohen Body, nicht das geparste JSON. Signaturen, die älter als 300 Sekunden sind, werden abgelehnt.

```
import express from "express";
import { verifyWebhook } from "sendtide";

app.post("/webhooks/sendtide", express.raw({ type: "application/json" }), (req, res) => {
  try {
    const event = verifyWebhook(req.body, req.get("Sendtide-Signature"), process.env.SENDTIDE_WEBHOOK_SECRET);
    console.log(event.type, event.data);
    res.sendStatus(200);
  } catch {
    res.sendStatus(400);
  }
});
```

```
from flask import Flask, request
from sendtide import SendtideError, verify_webhook

app = Flask(__name__)

@app.post("/webhooks/sendtide")
def sendtide_webhook():
    try:
        event = verify_webhook(request.get_data(), request.headers.get("Sendtide-Signature"), WEBHOOK_SECRET)
    except SendtideError:
        return "", 400
    print(event["type"], event["data"])
    return "", 200
```

```
use Sendtide\SendtideError;
use Sendtide\Webhook;

try {
    $event = Webhook::verify(
        file_get_contents('php://input'),
        $_SERVER['HTTP_SENDTIDE_SIGNATURE'] ?? null,
        getenv('SENDTIDE_WEBHOOK_SECRET'),
    );
} catch (SendtideError $e) {
    http_response_code(400);
    exit;
}
http_response_code(200);
```

Zum Testen baust du einen Header mit signWebhook(secret, unixSeconds, body) (Node.js), sign_webhook(...) (Python) oder Webhook::sign(...) (PHP). Wie die Signatur ohne SDK funktioniert, steht unter [Webhooks](https://sendtide.ch/docs/webhooks/#signatur-pr%C3%BCfen).
## Pagination

Listen liefern {data, next_cursor}. Die …All -Funktionen blättern selbst:

```
for await (const mail of sendtide.emails.searchAll({ q: "status:bounced" })) console.log(mail.id);
```

```
for mail in client.emails.search_all("status:bounced"):
    print(mail["id"])
```

```
foreach ($sendtide->emails->searchAll(['q' => 'status:bounced']) as $mail) { echo $mail['id']; }
```
## Fehler

Jeder Fehler wird als SendtideError ausgelöst. Er trägt status (0, wenn keine HTTP-Antwort kam), code, message, field und die Request-Id der Antwort (requestId, in Python auch request_id).

```
import { SendtideError } from "sendtide";

try {
  await sendtide.emails.send({ from: "hello@example.ch", to: "keine-adresse", subject: "Hi", text: "Hi" });
} catch (err) {
  if (err instanceof SendtideError) console.error(err.status, err.code, err.field, err.requestId);
  // 422 validation_error to[0] req_…
}
```

```
from sendtide import SendtideError

try:
    client.emails.send({"from": "hello@example.ch", "to": "keine-adresse", "subject": "Hi", "text": "Hi"})
except SendtideError as e:
    print(e.status, e.code, e.field, e.request_id)  # 422 validation_error to[0] req_…
```

```
use Sendtide\SendtideError;

try {
    $sendtide->emails->send(['from' => 'hello@example.ch', 'to' => 'keine-adresse', 'subject' => 'Hi', 'text' => 'Hi']);
} catch (SendtideError $e) {
    echo $e->status, ' ', $e->code, ' ', $e->field, ' ', $e->requestId;
}
```
## Ressourcen

Alle Gruppen der API sind als Ressourcen vorhanden, etwa emails, batches, templates, domains, webhooks, inbound, suppressions, tenants, streams, keys, exports und stats. Die genauen Methoden stehen in der README des SDKs, die Endpunkte in der [API-Referenz](https://sendtide.ch/docs/api/).
