Create New Email Address API
Note: Address JWT vs User JWT
This page describes Address JWT, which is different from User JWT:
Address JWT: Returned when creating a mailbox via
/api/new_addressor/admin/new_address- Use
Authorization: Bearer <jwt>header - Access
/api/*endpoints (view mails, delete mails, etc.)
- Use
User JWT: Obtained via
/user_api/loginor/user_api/register- Use
x-user-token: <jwt>header - Access
/user_api/*endpoints (user account management)
- Use
Do not confuse these two JWT types!
Create Email Address via Admin API
This is a python example using the requests library to send emails.
res = requests.post(
# Replace xxxx.xxxx with your worker domain
"https://xxxx.xxxx/admin/new_address",
json={
# Enable prefix (True/False)
"enablePrefix": True,
"name": "<email_name>",
"domain": "<email_domain>",
# Optional: ISO 8601 string or epoch ms; omit or null for never expiring
# "expiresAt": "2026-12-31T23:59:59Z",
},
headers={
'x-admin-auth': "<your_website_admin_password>",
# "x-custom-auth": "<your_website_password>", # If private site password is enabled
"Content-Type": "application/json"
}
)
# Returns {"jwt": "<Jwt>", "address": "<email_address>", "address_id": 123}
print(res.json())Optional Expiration Time (expiresAt)
This feature is controlled by ENABLE_ADDRESS_EXPIRY (default false):
- Migrate Before Enabling: Existing deployments must run the
v0.0.11database migration (Admin -> Quick Setup -> Database -> Migrate Database,POST /admin/db_migration, ordb/2026-11-18-address-expires-at.sql) before settingENABLE_ADDRESS_EXPIRY = true. For an existing database without a version record, call the migration endpoint directly instead of initializing it again. - Disabled Behavior: When
ENABLE_ADDRESS_EXPIRYisfalse(default), expiry UI controls are hidden, expiry checks and cleanup are paused (without erasing any previously savedexpires_atvalues), and explicitly passingexpiresAtreturns an error (403) rather than silently creating a permanent mailbox.
When ENABLE_ADDRESS_EXPIRY = true, both /api/new_address and /admin/new_address accept the optional expiresAt field:
- Omit or pass
null(default) so the address never expires. expiresAt: Pass future Unix epoch milliseconds or an ISO 8601 timestamp with a time zone (e.g."2026-12-31T23:59:59Z"). The database formatYYYY-MM-DD HH:mm:ssis also accepted as UTC. The deadline must be before2100-01-01T00:00:00Z. The web UI converts duration presets to this field at submission.- Once expired, shared mailbox authentication/login and single-address user actions reject access immediately. Inbound mail still follows
blockReceiveUnknowAddressEmailuntil cleanup removes the address. - Actual deletion of expired addresses and their mails happens in bounded batches during the scheduled cron (
expiredAddress) when[triggers]cronsis enabled inwrangler.toml(commented out by default inwrangler.toml.template), so exact removal timing depends on your cron schedule and backlog progress.
Create a Subdomain Mailbox Address
If your base domain is already configured in DOMAINS / DEFAULT_DOMAINS / USER_ROLES, and ENABLE_CREATE_ADDRESS_SUBDOMAIN_MATCH is enabled (it can also be toggled in the admin panel), the create-address APIs can accept subdomains directly:
res = requests.post(
"https://xxxx.xxxx/admin/new_address",
json={
"enablePrefix": True,
"name": "project001",
"domain": "team.example.com",
},
headers={
'x-admin-auth': "<your_website_admin_password>",
"Content-Type": "application/json"
}
)- If
example.comis an allowed base domain,team.example.comanddev.team.example.comcan match successfully - Lookalike domains such as
badexample.comwill not be treated asexample.com - This is different from
RANDOM_SUBDOMAIN_DOMAINS: here the caller explicitly specifies the subdomain, instead of the system generating a random one - In the admin panel, this can be set to Follow Environment Variable / Force Enable / Force Disable. Choosing Follow Environment Variable clears the admin override and returns to env fallback behavior.
Batch Create Random Username Email Addresses API Example
Batch Create Email Addresses via Admin API
This is a python example using the requests library to send emails.
import requests
import random
import string
from concurrent.futures import ThreadPoolExecutor, as_completed
def generate_random_name():
# Generate 5 lowercase letters
letters1 = ''.join(random.choices(string.ascii_lowercase, k=5))
# Generate 1-3 digits
numbers = ''.join(random.choices(string.digits, k=random.randint(1, 3)))
# Generate 1-3 lowercase letters
letters2 = ''.join(random.choices(string.ascii_lowercase, k=random.randint(1, 3)))
# Combine into final name
return letters1 + numbers + letters2
def fetch_email_data(name):
try:
res = requests.post(
"https://<worker_domain>/admin/new_address",
json={
"enablePrefix": True,
"name": name,
"domain": "<email_domain>",
},
headers={
'x-admin-auth': "<your_website_admin_password>",
# "x-custom-auth": "<your_website_password>", # If private site password is enabled
"Content-Type": "application/json"
}
)
if res.status_code == 200:
response_data = res.json()
email = response_data.get("address", "no address")
jwt = response_data.get("jwt", "no jwt")
return f"{email}----{jwt}\n"
else:
print(f"Request failed, status code: {res.status_code}")
return None
except requests.RequestException as e:
print(f"Request error: {e}")
return None
def generate_and_save_emails(num_emails):
with ThreadPoolExecutor(max_workers=30) as executor, open('email.txt', 'a') as file:
futures = [executor.submit(fetch_email_data, generate_random_name()) for _ in range(num_emails)]
for future in as_completed(futures):
result = future.result()
if result:
file.write(result)
# Generate 10 emails and append to existing file
generate_and_save_emails(10)Random address for a logged-in user
POST /user_api/random_address with the user JWT (x-user-token). Creates a random address and binds it to the current user.
- Body:
{"domain": "<mail domain, optional>", "cf_token": "<required when Turnstile is configured>"} - Response:
{"address": "<address>", "address_id": 123, "password": null} - Requires
ENABLE_USER_CREATE_EMAIL = true; the user's address limit, role domains and prefix rules apply.