uSend Docs
Quickstarts

Send with Deno Deploy

Send email from Deno Deploy using fetch and an environment variable.

You need a verified domain and an API key. Deno needs no SDK: fetch is built in.

1. Write the server

main.ts
const apiKey = Deno.env.get('USEND_API_KEY');

async function sendEmail(to: string) {
  const res = await fetch('https://api.usend.et/emails', {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${apiKey}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      from: 'Acme <[email protected]>',
      to: [to],
      subject: 'Hello from Deno',
      html: '<p>It works!</p>',
    }),
  });
  return { ok: res.ok, body: await res.json() };
}

Deno.serve(async (req) => {
  if (req.method !== 'POST') return new Response('POST only', { status: 405 });
  const { email } = await req.json();
  const { ok, body } = await sendEmail(email);
  return Response.json(body, { status: ok ? 200 : 502 });
});

On success the body is { "id": "..." }; on failure it is { "statusCode", "name", "message" }.

2. Run locally

Shell
USEND_API_KEY=us_123456789 deno run --allow-net --allow-env main.ts
curl -X POST localhost:8000 -d '{"email":"[email protected]"}'

3. Deploy

Shell
deno deploy --prod

Add USEND_API_KEY as an environment variable in the project's settings on Deno Deploy, not in the code.

Using the SDK

The SDK also runs on Deno through npm specifiers:

TypeScript
import { Usend } from 'npm:@usend/node';

const usend = new Usend(Deno.env.get('USEND_API_KEY'));
const { data, error } = await usend.emails.send({
  from: 'Acme <[email protected]>',
  to: ['[email protected]'],
  subject: 'Hello',
  text: 'Hi',
});

See Send emails for every field, and Errors for the error names.