Send route in Express.js
Add a POST route in Express.js that sends emails with the Publiq SDK, with error handling.
Instantiate the client once and reuse it across requests:
import express from 'express';
import { Publiq, PubliqError } from 'publiq';
const app = express();
app.use(express.json());
const publiq = new Publiq(process.env.PUBLIQ_API_KEY);
app.post('/send', async (req, res) => {
try {
const email = await publiq.emails.send({
from: 'you@yourdomain.com',
to: req.body.to,
templateKey: 'welcome-email',
});
res.json({ id: email.id });
} catch (err) {
if (err instanceof PubliqError) return res.status(err.status).json({ error: err.code });
res.status(500).json({ error: 'internal' });
}
});
app.listen(3000);Best practices
- Always call Publiq from the server — never expose the API key on the client.
- Create the client instance once and reuse it across requests.
- Prefer
templateKeyover inline HTML to keep content versioned. - Handle
PubliqError(status/code); the SDK already retries transient errors with backoff. - Send from a verified domain — see Domains.