diff --git a/.env.example b/.env.example
index ac1e492..8efd89a 100644
--- a/.env.example
+++ b/.env.example
@@ -20,6 +20,10 @@ REDIS_URL=redis://localhost:6379
# Optional: Spamhaus Data Query Service key. Public DNSBL queries from cloud resolvers are refused, so blacklist checks report "unknown" without it.
SPAMHAUS_DQS_KEY=
+# Optional: token (16+ chars) that status-page monitors send as "Authorization: Bearer
Thanks for signing up!
", + text: "Thanks for signing up!", +}); + +if (error) { + console.error("Send failed:", error); + process.exit(1); +} + +console.log("Queued email", data.emailId); +``` + +```sh +npm install +node send.js +``` + +Thanks for your order.
", + replyTo: "billing@acme.com", +}); + +if (error) throw new Error(error.message); +console.log("Queued email", data?.emailId); +``` + +```sh +npm install +npm run send +``` + +--- + +## React Email (TypeScript) + +Pass a React Email component as `react` and the SDK renders it to HTML for you — no separate render step needed. + +```json package.json +{ + "type": "module", + "scripts": { + "send": "tsx send.tsx" + }, + "dependencies": { + "bytesend-js": "latest", + "@react-email/components": "latest", + "react": "latest" + }, + "devDependencies": { + "@types/react": "latest", + "tsx": "latest", + "typescript": "latest" + } +} +``` + +```json tsconfig.json +{ + "compilerOptions": { + "target": "ES2022", + "module": "ESNext", + "moduleResolution": "Bundler", + "jsx": "react-jsx", + "strict": true, + "skipLibCheck": true + } +} +``` + +```tsx emails/welcome.tsx +import { Body, Button, Container, Heading, Html, Text } from "@react-email/components"; + +export function WelcomeEmail({ name, url }: { name: string; url: string }) { + return ( + + +Thanks for signing up!
", + }); + + if (error) { + return Response.json({ error: error.message }, { status: 502 }); + } + return Response.json({ emailId: data?.emailId }); +} +``` + +```ts app/api/bytesend-webhook/route.ts +import { Webhooks } from "bytesend-js"; + +const webhooks = new Webhooks(process.env.BYTESEND_WEBHOOK_SECRET!); + +export async function POST(request: Request) { + // Verify against the raw body — parsing JSON first breaks the signature. + const rawBody = await request.text(); + + try { + const event = webhooks.constructEvent(rawBody, { headers: request.headers }); + + if (event.type === "email.bounced") { + // Mark the address as undeliverable in your own database + } + + return new Response("ok"); + } catch (error) { + return new Response((error as Error).message, { status: 400 }); + } +} +``` + +--- + +## Express + +```ts server.ts +import express from "express"; +import { ByteSend, Webhooks } from "bytesend-js"; + +const bytesend = new ByteSend(); +const webhooks = new Webhooks(process.env.BYTESEND_WEBHOOK_SECRET!); +const app = express(); + +app.post("/welcome", express.json(), async (req, res) => { + const { data, error } = await bytesend.emails.send({ + from: "AcmeThanks for signing up!
", + }); + + if (error) return res.status(502).json({ error: error.message }); + res.json({ emailId: data?.emailId }); +}); + +// The webhook route needs the raw body for signature verification. +app.post("/bytesend-webhook", express.raw({ type: "application/json" }), (req, res) => { + try { + const event = webhooks.constructEvent(req.body, { headers: req.headers }); + if (event.type === "email.complained") { + // Stop emailing this recipient + } + res.send("ok"); + } catch (error) { + res.status(400).send((error as Error).message); + } +}); + +app.listen(3000); +``` + +--- + +## Safe retries and batches + +Pass an idempotency key to make retries safe: sending the same payload with the same key returns the original response instead of sending twice. + +```ts +await bytesend.emails.send( + { + from: "AcmeOrder #1042
", + }, + { idempotencyKey: "receipt-1042" }, +); + +await bytesend.emails.batch( + [ + { from: "AcmeHi A
" }, + { from: "AcmeHi B
" }, + ], + { idempotencyKey: "welcome-batch-7" }, +); +``` + +--- + +## Python + +```sh +pip install bytesend +``` + +```python send.py +from bytesend import ByteSend + +client = ByteSend() # reads BYTESEND_API_KEY + +resp, _ = client.emails.send(payload={ + "from": "AcmeThanks for signing up!
", +}) + +print("Queued email", resp["emailId"]) +``` + +See the [Python guide](/get-started/python) for error handling, idempotency and batches. + +--- + +## Go + +```sh +go get github.com/nodebyteltd/bytesend-go +``` + +```go main.go +package main + +import ( + "context" + "fmt" + "log" + + bytesend "github.com/nodebyteltd/bytesend-go" +) + +func main() { + client, err := bytesend.NewClient("") // empty key reads BYTESEND_API_KEY + if err != nil { + log.Fatal(err) + } + + resp, err := client.Emails.Create(context.Background(), bytesend.SendEmailPayload{ + From: "AcmeThanks for signing up!
", + }) + if err != nil { + log.Fatal(err) + } + + fmt.Println("Queued email", resp.EmailID) +} +``` + +See the [Go guide](/get-started/go) for the full client. diff --git a/apps/docs/guides/use-with-react-email.mdx b/apps/docs/guides/use-with-react-email.mdx index 38c7556..539430a 100644 --- a/apps/docs/guides/use-with-react-email.mdx +++ b/apps/docs/guides/use-with-react-email.mdx @@ -11,82 +11,83 @@ description: "A guide on how to use ByteSend with React Email"