@artamail/nextjs
Next.js SDK
Official ArtaMail integration for Next.js 13+ applications with App Router support.
Installation
Terminal
pnpm add @artamail/nextjsConfiguration
Add your API key to environment variables:
.env.local
ARTAMAIL_API_KEY=am_live_sk_your_key_here# Optional: custom API URLARTAMAIL_BASE_URL=https://artamail.artatol.comServer Components
app/templates/page.tsx
1import { getArtaMail } from '@artamail/nextjs/server';23export default async function TemplatesPage() {4 const artamail = getArtaMail();5 const templates = await artamail.listTemplates();67 return (8 <ul>9 {templates.map(t => (10 <li key={t.id}>{t.name}</li>11 ))}12 </ul>13 );14}Server Actions
app/actions.ts
1'use server'23import { sendEmail } from '@artamail/nextjs/server';45export async function sendWelcomeEmail(email: string, name: string) {6 const result = await sendEmail({7 to: email,8 template: 'welcome',9 data: { name }10 });1112 return { success: true, emailId: result.id };13}Use the action in a Client Component:
app/signup/page.tsx
'use client'import { sendWelcomeEmail } from '../actions';export default function SignupPage() { async function handleSubmit(formData: FormData) { const email = formData.get('email') as string; const name = formData.get('name') as string; const result = await sendWelcomeEmail(email, name); } return ( <form action={handleSubmit}> <input name="name" placeholder="Name" /> <input name="email" placeholder="Email" /> <button type="submit">Sign Up</button> </form> );}API Routes
app/api/send-email/route.ts
1import { sendEmail } from '@artamail/nextjs/server';2import { NextResponse } from 'next/server';34export async function POST(request: Request) {5 const { to, template, data } = await request.json();67 const result = await sendEmail({8 to,9 template,10 data11 });1213 return NextResponse.json({14 success: true,15 id: result.id,16 testMode: result.testMode17 });18}Available Functions
getArtaMail()
Get a singleton ArtaMail client instance. Cached for efficiency.
typescript
const artamail = getArtaMail();// With custom configconst artamail = getArtaMail({ apiKey: 'am_live_sk_different_key', timeout: 60000});sendEmail()
Convenience function to send a single email.
typescript
await sendEmail({ to: '[email protected]', template: 'welcome', data: { name: 'John' }, from: '[email protected]', fromName: 'My App'});sendBatchEmails()
Send multiple emails in a single batch (up to 1000).
typescript
const result = await sendBatchEmails({ emails: [ { to: '[email protected]', template: 'notification', data: { ... } }, { to: '[email protected]', template: 'notification', data: { ... } }, ]});listSenderAddresses()
Get available sender addresses for your organization.
typescript
const addresses = await listSenderAddresses();const defaultAddress = addresses.find(a => a.isDefault);Best Practices
Server-Side Only
The
@artamail/nextjs/server module should only be imported in server-side code. Never import it in Client Components.- Use Server Actions for form submissions to keep API keys secure
- Use the singleton (
getArtaMail()) for most cases - Handle errors gracefully and show user-friendly messages
- Use test keys during development to avoid sending real emails