Security
Authentication
Learn how to authenticate your API requests using API keys and best practices for keeping your keys secure.
API Keys
Unosend uses API keys to authenticate requests. You can create and manage API keys in your dashboard under API Keys.
API Key Format
All Unosend API keys are prefixed with un_ for easy identification:
un_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxUsing the Authorization Header
Include your API key in the Authorization header as a Bearer token:
HTTP Header
Authorization: Bearer un_your_api_keyExample Request
cURL
curl -X POST https://www.unosend.co/api/v1/emails \
-H "Authorization: Bearer un_your_api_key" \
-H "Content-Type: application/json" \
-d '{
"from": "hello@yourdomain.com",
"to": ["recipient@example.com"],
"subject": "Hello World",
"html": "<h1>Hello!</h1>"
}'Code Examples
Here's how to authenticate in different languages:
Node.js (fetch)
const response = await fetch('https://www.unosend.co/api/v1/emails', {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.UNOSEND_API_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
from: 'hello@yourdomain.com',
to: ['recipient@example.com'],
subject: 'Hello World',
html: '<h1>Hello!</h1>'
})
});Python
import os
import requests
response = requests.post(
'https://www.unosend.co/api/v1/emails',
headers={
'Authorization': f'Bearer {os.environ["UNOSEND_API_KEY"]}',
'Content-Type': 'application/json'
},
json={
'from': 'hello@yourdomain.com',
'to': ['recipient@example.com'],
'subject': 'Hello World',
'html': '<h1>Hello!</h1>'
}
)PHP
<?php
$ch = curl_init('https://www.unosend.co/api/v1/emails');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . getenv('UNOSEND_API_KEY'),
'Content-Type: application/json'
],
CURLOPT_POSTFIELDS => json_encode([
'from' => 'hello@yourdomain.com',
'to' => ['recipient@example.com'],
'subject' => 'Hello World',
'html' => '<h1>Hello!</h1>'
])
]);
$response = curl_exec($ch);Creating API Keys
To create a new API key:
- Go to your Dashboard → API Keys
- Click "Create API Key"
- Enter a descriptive name (e.g., "Production Server")
- Copy the key immediately - it's only shown once!
API Response
{
"id": "550e8400-e29b-41d4-a716-446655440000",
"name": "Production Server",
"key": "un_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"key_prefix": "un_xxxx",
"created_at": "2024-01-15T10:30:00.000Z"
}Important: The full API key is only returned once when created. Store it securely - you won't be able to see it again!
Security Best Practices
Keep your API keys secure
- • Never expose API keys in client-side code (frontend)
- • Store keys in environment variables, not in code
- • Don't commit API keys to version control
- • Use different keys for development and production
- • Rotate keys regularly and revoke unused keys
Using Environment Variables
Store your API key in an environment variable for security:
.env.local
UNOSEND_API_KEY=un_your_api_keyAdd .env.local to your .gitignore:
.gitignore
.env.local
.env*.localAuthentication Errors
If authentication fails, you'll receive one of these error responses:
401 Unauthorized
{
"error": {
"message": "Invalid or missing API key",
"code": 401
}
}