Prerequisites
- A thePurplebox account
- A secret API key generated from your dashboard
- A verified sending domain (recommended for best deliverability)
Sending an email
Sending a transactional email requires one API call with three basic parameters:to: A single email address or an array of recipientssubject: The email subject linebody: The message content (plain text or HTML)
- JavaScript
- Go
- Python
- PHP
- Java
- Rust
- Ruby
- C#
- ASP.NET
- cURL
Copy
await fetch('https://api.thepurplebox.io/v1/send', {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer <API_KEY>"
},
body: JSON.stringify({
to: "[email protected]",
subject: "Welcome to thePurplebox!",
body: "<h1>Your account is ready!</h1>"
})
})
Copy
package main
import (
"bytes"
"encoding/json"
"net/http"
)
func main() {
payload := map[string]interface{}{
"to": "[email protected]",
"subject": "Welcome to thePurplebox!",
"body": "<h1>Your account is ready!</h1>",
}
jsonData, _ := json.Marshal(payload)
req, _ := http.NewRequest("POST", "https://api.thepurplebox.io/v1/send", bytes.NewBuffer(jsonData))
req.Header.Set("Authorization", "Bearer <API_KEY>")
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
client.Do(req)
}
Copy
import requests
url = "https://api.thepurplebox.io/v1/send"
payload = {
"to": "[email protected]",
"subject": "Welcome to thePurplebox!",
"body": "<h1>Your account is ready!</h1>"
}
headers = {
"Authorization": "Bearer <API_KEY>",
"Content-Type": "application/json"
}
requests.post(url, json=payload, headers=headers)
Copy
<?php
$url = "https://api.thepurplebox.io/v1/send";
$payload = [
"to" => "[email protected]",
"subject" => "Welcome to thePurplebox!",
"body" => "<h1>Your account is ready!</h1>"
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Bearer <API_KEY>",
"Content-Type: application/json"
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
?>
Copy
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.URI;
public class SendEmail {
public static void main(String[] args) throws Exception {
String json = """
{
"to": "[email protected]",
"subject": "Welcome to thePurplebox!",
"body": "<h1>Your account is ready!</h1>"
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.thepurplebox.io/v1/send"))
.header("Content-Type", "application/json")
.header("Authorization", "Bearer <API_KEY>")
.POST(HttpRequest.BodyPublishers.ofString(json))
.build();
HttpResponse<String> response = client.send(request,
HttpResponse.BodyHandlers.ofString());
}
}
Copy
use reqwest;
use serde_json::json;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = reqwest::Client::new();
let payload = json!({
"to": "[email protected]",
"subject": "Welcome to thePurplebox!",
"body": "<h1>Your account is ready!</h1>"
});
let response = client
.post("https://api.thepurplebox.io/v1/send")
.header("Content-Type", "application/json")
.header("Authorization", "Bearer <API_KEY>")
.json(&payload)
.send()
.await?;
Ok(())
}
Copy
require 'net/http'
require 'json'
require 'uri'
uri = URI('https://api.thepurplebox.io/v1/send')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri)
request['Content-Type'] = 'application/json'
request['Authorization'] = 'Bearer <API_KEY>'
request.body = {
to: '[email protected]',
subject: 'Welcome to thePurplebox!',
body: '<h1>Your account is ready!</h1>'
}.to_json
response = http.request(request)
Copy
using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
public class SendEmail
{
public static async Task Main()
{
var client = new HttpClient();
client.DefaultRequestHeaders.Add("Authorization", "Bearer <API_KEY>");
var payload = new
{
to = "[email protected]",
subject = "Welcome to thePurplebox!",
body = "<h1>Your account is ready!</h1>"
};
var json = JsonSerializer.Serialize(payload);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await client.PostAsync(
"https://api.thepurplebox.io/v1/send",
content
);
}
}
Copy
using Microsoft.AspNetCore.Mvc;
using System.Net.Http;
using System.Text;
using System.Text.Json;
[ApiController]
[Route("api/[controller]")]
public class EmailController : ControllerBase
{
private readonly IHttpClientFactory _httpClientFactory;
public EmailController(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory;
}
[HttpPost("send")]
public async Task<IActionResult> SendEmail()
{
var client = _httpClientFactory.CreateClient();
client.DefaultRequestHeaders.Add("Authorization", "Bearer <API_KEY>");
var payload = new
{
to = "[email protected]",
subject = "Welcome to thePurplebox!",
body = "<h1>Your account is ready!</h1>"
};
var json = JsonSerializer.Serialize(payload);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await client.PostAsync(
"https://api.thepurplebox.io/v1/send",
content
);
if (response.IsSuccessStatusCode)
{
return Ok(new { message = "Email sent successfully" });
}
return BadRequest(new { message = "Failed to send email" });
}
}
Copy
curl -X POST https://api.thepurplebox.io/v1/send \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <API_KEY>" \
-d '{
"to": "[email protected]",
"subject": "Welcome to thePurplebox!",
"body": "<h1>Your account is ready!</h1>"
}'