Documentation
Everything you need to call, deploy and operate the API
Quick start
From a fresh Entra ID app registration to a delivered email.
- 1
Register an app in Microsoft Entra ID
Create an app registration, grant it the Mail.Send application permission under Microsoft Graph, then have a tenant admin click Grant admin consent. Note the Application (client) ID, Directory (tenant) ID and a client secret value.
- 2
Get an API key
Ask your Unisend admin for a per-caller API key (usk_live_…) from Keys & secrets. Every request to the API carries it in the x-api-key header.
- 3
Send your first email
Exchange your Azure AD credentials for an app-only token via POST /v1/token, then call POST /v1/messages with the mailbox to send from and the message body. Attachments travel as base64 inline, or as an upload-session URL above 3 MB.
# 1. Get an app-only access token from Azure AD
curl -s -X POST https://mail-api.contoso-logistics.com/v1/token \
-H "x-api-key: usk_live_7Hq2mNq1Tz9kR4pL" \
-H "Content-Type: application/json" \
-d '{
"appId": "b8d3e6f1-2a4c-4f9b-8e7d-6c1a0f5e9b32",
"tenantId": "4f2c9a1e-7b3d-4e8a-9c61-2d5f0b8e3a17"
}' | tee token.json
ACCESS_TOKEN=$(grep -o '"accessToken":"[^"]*"' token.json | cut -d'"' -f4)
echo "token acquired, expires in $(grep -o '"expiresIn":[0-9]*' token.json | cut -d: -f2)s"
# 2. Base64-encode the attachment
base64 -w0 invoice.pdf > invoice.b64
# 3. Send your first email, with the PDF attached
curl -s -X POST https://mail-api.contoso-logistics.com/v1/messages \
-H "x-api-key: usk_live_7Hq2mNq1Tz9kR4pL" \
-H "Content-Type: application/json" \
-d "{
\"callerEmail\": \"noreply@contoso-logistics.com\",
\"appId\": \"b8d3e6f1-2a4c-4f9b-8e7d-6c1a0f5e9b32\",
\"tenantId\": \"4f2c9a1e-7b3d-4e8a-9c61-2d5f0b8e3a17\",
\"mailbox\": \"m365\",
\"from\": \"noreply@contoso-logistics.com\",
\"to\": [\"j.martin@northwind.eu\"],
\"subject\": \"Shipment CL-48213 out for delivery\",
\"body\": { \"contentType\": \"html\", \"content\": \"<p>Your shipment left our Rotterdam hub and is on its way.</p>\" },
\"attachments\": [
{ \"name\": \"invoice.pdf\", \"contentType\": \"application/pdf\", \"contentBase64\": \"$(cat invoice.b64)\" }
]
}"Authentication
Every request carries an x-api-key header; the API itself talks to Azure AD with the client credentials grant.
x-api-key identifies the calling service to Unisend (used for logging, rate limits and caller scopes). It is not the same thing as an Azure AD token: Unisend mints that token for you behind the scenes against POST /v1/token, using your app registration's appId / tenantId / clientSecret.
Client credentials flow
Your app
calls Unisend
Unisend API
x-api-key + token cache
Azure AD
client credentials grant
Graph / Dataverse
M365 or D365 mailbox
Token caching: tokens are cached per appId + tenantId pair and refreshed 5 minutes before they expire, so a burst of sends never triggers a token request per message.
API reference
Every endpoint exposed by Unisend. Expand a card for params and samples.
Exchange your Azure AD app registration for an app-only access token (client credentials grant).
| Name | Type | Required | Description |
|---|---|---|---|
| appId | string | required | Application (client) ID of the Entra ID app registration. |
| tenantId | string | required | Directory (tenant) ID the app is registered in. |
| clientSecret | string | optional | Client secret value. Optional when the secret is resolved server-side from Azure Key Vault. |
Sample request
{ "appId": "b8d3e6f1-2a4c-4f9b-8e7d-6c1a0f5e9b32", "tenantId": "4f2c9a1e-7b3d-4e8a-9c61-2d5f0b8e3a17" }
Sample response
{ "accessToken": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIs...", "tokenType": "Bearer", "expiresIn": 3599, "scope": "https://graph.microsoft.com/.default", "cached": false }
Error codes
| HTTP | Code | Meaning |
|---|---|---|
| 400 | validation_error | Request body failed schema validation (missing field, bad enum value, malformed email). |
| 401 | invalid_client | AADSTS7000215 — invalid client secret, or the API key does not match any caller. |
| 403 | mailbox_access_denied | The app registration has Mail.Send but no application access policy grants it this specific mailbox. |
| 413 | never | Never returned: there is no request size limit at the API layer — attachments over 3 MB are streamed through Graph upload sessions. |
| 429 | throttled | Graph application throttling. Response carries a Retry-After header (seconds); the API already retries once internally. |
| 502 | graph_error | Microsoft Graph or Dataverse returned an unexpected error. The raw upstream error is included in the response body. |
Mailboxes: Microsoft 365 vs Dynamics 365
Same mailbox field, two very different delivery and logging paths.
| Aspect | m365 | d365 |
|---|---|---|
| How it sends | Microsoft Graph /sendMail on the shared mailbox | Dataverse email activity, set to Pending Send, picked up by the D365 server-side sync mailbox |
| Permissions needed | Graph application permission Mail.Send + admin consent, scoped to the mailbox via an application access policy | A Dataverse application user bound to the Entra ID app, with a security role granting Create/Append on email, plus server-side sync enabled on the mailbox |
| What gets logged | Send status + Graph request ID in Unisend's own log store | Same, plus the email is a first-class Dataverse email activity record (regarding a case, opportunity, etc.) |
| Attachments path | Inline base64 on /sendMail up to ~3 MB; Graph upload session + attach step above that | Base64 on the Dataverse email activity below ~3 MB; chunked Dataverse file upload for larger attachments |
Deployment guide
Ship Unisend to Azure App Service, or as a container.
- 1Create a resource group for the API and its supporting resources.
- 2Create a Linux App Service plan.
- 3Create the Web App on Node 20 LTS.
- 4Assign a system-managed identity to the Web App so it can read secrets without a stored credential.
- 5Grant that identity get/list access on the Key Vault that holds the client secrets.
- 6Push the app settings (Key Vault URI, default tenant/app IDs, log retention) and deploy.
# 1. Resource group az group create \ --name rg-unisend-api \ --location westeurope # 2. Linux App Service plan az appservice plan create \ --name plan-unisend-api \ --resource-group rg-unisend-api \ --sku B1 \ --is-linux # 3. Web App on Node 20 LTS az webapp create \ --name unisend-api-contoso \ --resource-group rg-unisend-api \ --plan plan-unisend-api \ --runtime "NODE:20-lts" # 4. System-managed identity az webapp identity assign \ --name unisend-api-contoso \ --resource-group rg-unisend-api # 5. Grant it access to Key Vault secrets az keyvault set-policy \ --name kv-unisend-contoso \ --object-id $(az webapp identity show -n unisend-api-contoso -g rg-unisend-api --query principalId -o tsv) \ --secret-permissions get list # 6. App settings + deploy az webapp config appsettings set \ --name unisend-api-contoso \ --resource-group rg-unisend-api \ --settings KEY_VAULT_URI="https://kv-unisend-contoso.vault.azure.net/" DEFAULT_TENANT_ID="4f2c9a1e-7b3d-4e8a-9c61-2d5f0b8e3a17" LOG_RETENTION_DAYS="90" az webapp deployment source config-zip \ --name unisend-api-contoso \ --resource-group rg-unisend-api \ --src dist.zip
CI/CD — GitHub Actions
name: deploy
on:
push:
branches: [main]
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: "20"
- name: Install & build
run: |
npm ci
npm run build
npm test
- name: Azure login
uses: azure/login@v2
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Deploy to Azure App Service
uses: azure/webapps-deploy@v3
with:
app-name: unisend-api-contoso
package: .Secret rotation
Zero-downtime dual-secret strategy: the new secret goes live before the old one disappears.
Changelog
What changed, newest first.
- Attachments over 3 MB now stream through Graph upload sessions automatically — no client-side change needed, same contentBase64 field.
- Dynamics 365 sends are now recorded as email activities in Dataverse, linked via d365.regardingObjectId / d365.regardingType.
- Initial release: /v1/token, /v1/messages (send + get + list), /health. Microsoft 365 shared-mailbox sending, per-caller API keys, Key Vault-backed secrets.
Was this page helpful?