--- name: ecommerce-email-support-nodejs title: "E-Commerce AI Email Support with Telnyx" description: "AI agent processes inbound customer emails — identifies issues, looks up orders, initiates replacements, and sends personalized replies in real time" language: nodejs framework: express telnyx_products: [Email, AI] --- # E-Commerce AI Email Support with Telnyx AI agent processes inbound customer emails — identifies issues, looks up orders, initiates replacements, and sends personalized replies in real time, all visible in a three-panel dashboard. ## Why Telnyx Telnyx provides the **AI Communications Infrastructure** behind this demo: the Email API receives and sends customer support emails, AI Inference powers the agentic reasoning loop that understands issues and takes action, and Server-Sent Events stream every AI decision to a live dashboard. Together they turn a manual, hours-long support workflow into an automated seconds-long resolution — the same pattern scales to returns processing, order status bots, and any email-driven support automation. ## Telnyx API Endpoints Used - **Email Send**: `POST /v2/emails` — [API reference](https://developers.telnyx.com/api/email/send-email) - **Email Webhook**: `email.received` event — inbound email processing - **AI Inference**: `POST /v2/ai/chat/completions` — [API reference](https://developers.telnyx.com/api/inference/inference) ## Architecture ``` Customer Email │ ▼ ┌──────────────┐ POST /v2/ai/chat/completions ┌────────────────┐ │ Express API │ ─────────────────────────────────► │ Telnyx AI │ │ + SQLite │ ◄──── tool_calls (agentic loop) ── │ Inference │ └──────┬───────┘ └────────────────┘ │ POST /v2/emails │ ───────────────────────────────────────────► Telnyx Email API │ └──► SSE stream ──► 3-panel dashboard (tickets + emails + AI reasoning) ``` 1. Customer sends a support email (or demo scenario triggers one) 2. Server creates a ticket and stores the inbound email 3. AI agent loop starts: system prompt + customer context + email body 4. AI calls tools (lookup customer, find order, check eligibility, initiate return, send email, resolve ticket) 5. Each tool execution publishes an SSE event to the dashboard 6. Dashboard renders the full AI reasoning chain in real time — customer profile, order status changes, and email thread update live ## Environment Variables Copy `.env.example` to `.env` and fill in: | Variable | Type | Example | Required | Description | Where to get it | |----------|------|---------|----------|-------------|-----------------| | `TELNYX_API_KEY` | `string` | `KEY0123456789ABCDEF` | no (demo mode) | Telnyx API v2 key | [Portal](https://portal.telnyx.com/api-keys) · [CLI: `telnyx auth`](https://developers.telnyx.com/development/cli) | | `FROM_EMAIL` | `string` | `support@yourdomain.com` | no | Sender address for outbound emails | Your verified sending domain | | `PORT` | `number` | `3000` | no | Server port | — | | `DEMO_MODE` | `boolean` | `true` | no | Enable demo mode (default: true) — works without credentials | — | | `AI_MODEL` | `string` | `meta-llama/Llama-3.3-70B-Instruct` | no | AI model for inference | [Telnyx AI models](https://developers.telnyx.com/docs/inference/models) | > **Demo mode** is enabled by default. The dashboard works with five one-click scenarios and mock AI inference — no Telnyx credentials needed. Set `DEMO_MODE=false` and provide a `TELNYX_API_KEY` for live email processing. > **Agent / CLI access** — provision resources programmatically with the [Telnyx CLI](https://developers.telnyx.com/development/cli): > > ```bash > telnyx auth login > telnyx api-keys list > ``` > > Full API discovery: [llms-full.txt](https://developers.telnyx.com/llms-full.txt) · [CLI docs](https://developers.telnyx.com/development/cli) ## Setup ```bash git clone https://github.com/team-telnyx/telnyx-code-examples.git cd telnyx-code-examples/ecommerce-email-support-nodejs cp .env.example .env # demo mode works out of the box npm install npm start # starts on http://localhost:3000 ```
Programmatic / CLI setup ```bash # Install CLI — https://developers.telnyx.com/development/cli go install github.com/team-telnyx/telnyx-cli/cmd/telnyx@latest telnyx auth login # Verify your API key works telnyx api-keys list ``` For full API discovery, point your agent at [`llms-full.txt`](https://developers.telnyx.com/llms-full.txt).
### Demo Mode (default) Open `http://localhost:3000` and click any of the five scenario buttons: | Scenario | Customer | Issue | AI Action | |----------|----------|-------|-----------| | **Damaged Item** | Maria Santos | Cracked headphones ($249.99) | Immediate replacement, no return needed | | **Wrong Size** | James Chen | Jacket ordered wrong size ($129.00) | Exchange with prepaid return label | | **Late Delivery** | Priya Patel | Smartwatch delayed ($329.00) | Tracking update + 15% discount code | | **Return Request** | Lukas Weber | Shoes don't fit ($159.00) | Full refund, prepaid label sent | | **Promo Code** | Noah Kim | Discount code not applied ($89.00) | Partial refund for missed discount | Each scenario runs the full AI workflow in ~8-12 seconds with animated reasoning steps. ### Live Mode (from dashboard) Even in demo mode, adding a `TELNYX_API_KEY` to `.env` enables the **Live Mode** button (purple pill in the header). Click it to compose a customer email directly from the dashboard — the AI agent processes it with **real Telnyx AI Inference** and sends a **real email reply** via the Telnyx Email API, all rendered in real time on the dashboard. 1. Add `TELNYX_API_KEY=KEYxxx` to `.env` (keep `DEMO_MODE=true`) 2. Restart the server: `npm start` 3. The purple **Live Mode** button appears enabled in the header 4. Click it, fill in a customer email / subject / body, and hit **Send & Process** 5. Watch the AI reasoning chain, tool calls, and outbound email appear in real time Demo scenarios (mock AI) and live compose (real AI) coexist — no ngrok or webhook configuration needed. ### Live Mode (webhook) Set `DEMO_MODE=false` in `.env`, add your `TELNYX_API_KEY`, and configure the inbound email webhook: 1. Expose your server: `ngrok http 3000` 2. Set webhook URL in [Telnyx Portal](https://portal.telnyx.com) → Email Settings → `https://.ngrok.io/webhooks/email` 3. Send an email to your Telnyx-connected address — the AI agent processes it automatically ## API Reference See [`API.md`](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/ecommerce-email-support-nodejs/API.md) for the full typed endpoint reference. ### Quick example — trigger a demo scenario ```bash curl -X POST http://localhost:3000/api/demo/trigger \ -H "Content-Type: application/json" \ -d '{"scenario": "damaged_item"}' ``` **Response:** ```json { "ticket_id": 1, "scenario": "damaged_item" } ``` ## Troubleshooting - **Dashboard shows no updates after clicking a scenario**: Check the browser console for SSE connection errors. The `/stream` endpoint must be reachable. - **"Missing TELNYX_API_KEY" on startup**: Either set `DEMO_MODE=true` (default) or provide a valid API key in `.env`. - **AI agent not responding in live mode**: Verify your API key has AI Inference access. Check the server logs for inference errors. - **Inbound emails not creating tickets**: Confirm the webhook URL is reachable from the internet and configured for `email.received` events. - **Port 3000 in use**: Start with another port: `PORT=3001 npm start`. - **SQLite error on startup**: Requires Node.js 22+ for the built-in `node:sqlite` module. ## Related Examples - [`email-batch-send-nodejs`](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/email-batch-send-nodejs/README.md) — batch email send with delivery tracking dashboard - [`ai-email-agent-python`](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/ai-email-agent-python/README.md) — autonomous inbound email reply bot - [`email-whatsapp-support-python`](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/email-whatsapp-support-python/README.md) — unified email + WhatsApp support with AI agent ## Agent Discovery This example is part of the [Telnyx Code Examples](https://github.com/team-telnyx/telnyx-code-examples) catalog. - **Agent signup**: [telnyx.com/agent-signup.md](https://telnyx.com/agent-signup.md) — automated account provisioning via agent mail; get an API key with no human intervention - **Agent CLI**: [github.com/team-telnyx/ai/tree/main/cli](https://github.com/team-telnyx/ai/tree/main/cli) — composite commands for agents ([commands reference](https://github.com/team-telnyx/ai/tree/main/cli/src/commands)) - **Agent skills**: [github.com/team-telnyx/ai/tree/main/skills](https://github.com/team-telnyx/ai/tree/main/skills) - **Telnyx AI repo**: [github.com/team-telnyx/ai](https://github.com/team-telnyx/ai) - **LLM-optimized docs**: [`llms-full.txt`](https://developers.telnyx.com/llms-full.txt) - **Example index**: [`llms.txt`](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/llms.txt) - **Telnyx CLI (human)**: [developers.telnyx.com/development/cli](https://developers.telnyx.com/development/cli) — `go install github.com/team-telnyx/telnyx-cli/cmd/telnyx@latest` ## Resources - [Email API Guide](https://developers.telnyx.com/docs/email) - [Email API Reference](https://developers.telnyx.com/api/email/send-email) - [AI Inference Guide](https://developers.telnyx.com/docs/inference) - [AI Inference API Reference](https://developers.telnyx.com/api/inference/inference) - [Node.js SDK](https://developers.telnyx.com/development/sdk/nodejs) - [Email API Product Page](https://telnyx.com/products/email-api) - [Email Pricing](https://telnyx.com/pricing/email)