# Sendtern SDKs

> Install a Sendtern SDK and send an email from your server.

Sendtern has SDKs for Node.js and TypeScript, Python, Ruby, PHP, Laravel, Go, Rust, Java, and .NET. Each supports sending, batch sending, retrieving an email by ID, and capturing a message in Email Playground.

## Install the SDK

Run the command in your project. Packages are distributed from the [Sendtern SDK release](https://github.com/Sendtern/sendtern-sdks/releases/tag/v0.2.0).

Node.js / TypeScript:

```
npm install https://github.com/Sendtern/sendtern-sdks/releases/download/v0.2.0/sendtern-sdk-0.2.0.tgz
```

Python:

```
python -m pip install https://github.com/Sendtern/sendtern-sdks/releases/download/v0.2.0/sendtern-0.2.0-py3-none-any.whl
```

Ruby:

```
curl -fLO https://github.com/Sendtern/sendtern-sdks/releases/download/v0.2.0/sendtern-0.2.0.gem
gem install --local sendtern-0.2.0.gem
```

PHP:

```
composer config repositories.sendtern vcs https://github.com/Sendtern/sendtern-sdks
composer require sendtern/sdk:0.2.0
```

Laravel:

```
composer config repositories.sendtern vcs https://github.com/Sendtern/sendtern-sdks
composer require sendtern/sdk:0.2.0
```

Go:

```
go get github.com/Sendtern/sendtern-sdks/go@v0.2.1
```

Rust:

```
cargo add sendtern --git https://github.com/Sendtern/sendtern-sdks --tag v0.2.0
cargo add serde_json
```

Java:

```
curl -fLO https://github.com/Sendtern/sendtern-sdks/releases/download/v0.2.0/sendtern-0.2.0.jar
```

.NET:

```
curl -fLO https://github.com/Sendtern/sendtern-sdks/releases/download/v0.2.0/Sendtern.0.2.0.nupkg
dotnet add package Sendtern --version 0.2.0 --source .
```

| SDK | Runtime |
| --- | --- |
| Node.js / TypeScript | Node.js 18+ |
| Python | Python 3.9+ |
| Ruby | Ruby 3.1+ |
| PHP | PHP 8.1+, cURL, Composer |
| Laravel | Laravel 12+, PHP cURL, Composer |
| Go | Go 1.21+ |
| Rust | Rust 1.88+ |
| Java | Java 17+ |
| .NET | .NET SDK 8+ |

For Java, save the example as `SendExample.java` in the same directory as the JAR and run `java --class-path sendtern-0.2.0.jar SendExample.java`.

For Laravel, add this entry to `config/services.php` and append the example to `routes/console.php`. Run `php artisan sendtern:example` from the project directory.

```
'sendtern' => [
    'key' => env('SENDTERN_API_KEY'),
    'base_url' => env('SENDTERN_BASE_URL', 'https://sendtern.com'),
],
```

## 发送您的第一封电子邮件

Create a [sending-only API key](https://sendtern.com/zh-hans/dashboard/api-keys) and store it on your server as `SENDTERN_API_KEY`. Replace the sender with an address on your [verified domain](https://sendtern.com/zh-hans/docs/domains) and use a recipient address you control.

Node.js / TypeScript:

```
import { Sendtern } from '@sendtern/sdk';

const client = new Sendtern(process.env.SENDTERN_API_KEY);
const result = await client.emails.send({
  "from": "Acme <hello@mail.example.com>",
  "to": "you@example.com",
  "subject": "Hello from Sendtern",
  "text": "This is your first Sendtern email."
});
console.log(result.id);
```

Python:

```
import os
from sendtern import Sendtern

client = Sendtern(os.environ["SENDTERN_API_KEY"])
result = client.emails.send({
  "from": "Acme <hello@mail.example.com>",
  "to": "you@example.com",
  "subject": "Hello from Sendtern",
  "text": "This is your first Sendtern email."
})
print(result["id"])
```

Ruby:

```
require "sendtern"

client = Sendtern::Client.new(ENV.fetch("SENDTERN_API_KEY"))
result = client.emails.send({
  from: "Acme <hello@mail.example.com>",
  to: "you@example.com",
  subject: "Hello from Sendtern",
  text: "This is your first Sendtern email."
})
puts result.fetch("id")
```

PHP:

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

$client = new Sendtern\Client(getenv("SENDTERN_API_KEY"));
$result = $client->send([
    'from' => 'Acme <hello@mail.example.com>',
    'to' => 'you@example.com',
    'subject' => 'Hello from Sendtern',
    'text' => 'This is your first Sendtern email.'
]);
echo $result["id"] . PHP_EOL;
```

Laravel:

```
\Illuminate\Support\Facades\Artisan::command("sendtern:example", function () {
    $client = app(\Sendtern\Client::class);
    $result = $client->send([
        'from' => 'Acme <hello@mail.example.com>',
        'to' => 'you@example.com',
        'subject' => 'Hello from Sendtern',
        'text' => 'This is your first Sendtern email.'
    ]);
    $this->info($result["id"]);
});
```

Go:

```
package main

import (
    "context"
    "fmt"
    "log"
    "os"

    "github.com/Sendtern/sendtern-sdks/go"
)

func main() {
    client, err := sendtern.New(os.Getenv("SENDTERN_API_KEY"), "")
    if err != nil { log.Fatal(err) }
    message := map[string]string{
        "from": "Acme <hello@mail.example.com>",
        "to": "you@example.com",
        "subject": "Hello from Sendtern",
        "text": "This is your first Sendtern email.",
    }
    result, err := client.Send(context.Background(), message, "")
    if err != nil { log.Fatal(err) }
    fmt.Println(result.ID)
}
```

Rust:

```
use sendtern::Client;
use serde_json::json;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let key = std::env::var("SENDTERN_API_KEY")?;
    let client = Client::new(key)?;
    let result = client.send(&json!({
      "from": "Acme <hello@mail.example.com>",
      "to": "you@example.com",
      "subject": "Hello from Sendtern",
      "text": "This is your first Sendtern email."
    }), None)?;
    println!("{}", result.id);
    Ok(())
}
```

Java:

```
import com.sendtern.Sendtern;
import java.util.Map;

public class SendExample {
    public static void main(String[] args) throws Exception {
        var client = new Sendtern(System.getenv("SENDTERN_API_KEY"));
        var message = Map.of(
            "from", "Acme <hello@mail.example.com>",
            "to", "you@example.com",
            "subject", "Hello from Sendtern",
            "text", "This is your first Sendtern email."
        );
        var result = client.send(message, null);
        System.out.println(result.id());
    }
}
```

.NET:

```
using Sendtern;

var key = Environment.GetEnvironmentVariable("SENDTERN_API_KEY")
    ?? throw new InvalidOperationException("Set SENDTERN_API_KEY");
var client = new SendternClient(key);
var message = new {
    from = "Acme <hello@mail.example.com>",
    to = "you@example.com",
    subject = "Hello from Sendtern",
    text = "This is your first Sendtern email."
};
var result = await client.SendAsync(message);
Console.WriteLine(result.Id);
```

The response contains an email ID. Open [Emails](https://sendtern.com/zh-hans/dashboard/emails) to follow delivery. A full-access key is required to fetch an email by ID through the API.

## Test without sending

Open the [Integration assistant](https://sendtern.com/zh-hans/dashboard/integration) and select your language for a sample that calls `capture` on the SDK. The message goes to [Email Playground](https://sendtern.com/zh-hans/dashboard/playground), which lets you inspect the HTML and headers before you configure a sending domain.

## Batches and delivery status

Batch submission uses strict validation by default. Use permissive validation to accept valid rows and read errors with the original indexes. Email lookup returns `last_event` and the delivery timeline in `events`.

## Handle errors and retries

SDK errors expose the HTTP status and the API error code when available. If your app retries a send or batch request, reuse the same idempotency key and payload. If a response includes `Retry-After`, wait for that interval before retrying.

See the [API reference](https://sendtern.com/zh-hans/docs/api-introduction) for request fields and error codes, or [send with cURL](https://sendtern.com/zh-hans/docs/send-with-curl) from any environment with HTTP access.
