├── .env.example ├── .gitignore ├── LICENSE ├── app ├── api │ └── send │ │ └── route.ts ├── layout.tsx └── page.tsx ├── components └── email-template.tsx ├── next-env.d.ts ├── next.config.js ├── package.json ├── readme.md ├── renovate.json └── tsconfig.json /.env.example: -------------------------------------------------------------------------------- 1 | RESEND_API_KEY="" -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .env 2 | .next 3 | node_modules -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Plus Five Five, Inc. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /app/api/send/route.ts: -------------------------------------------------------------------------------- 1 | import { EmailTemplate } from '../../../components/email-template'; 2 | import { Resend } from 'resend'; 3 | import * as React from 'react'; 4 | 5 | const resend = new Resend(process.env.RESEND_API_KEY); 6 | 7 | export async function POST() { 8 | try { 9 | const { data, error } = await resend.emails.send({ 10 | from: 'Acme ', 11 | to: ['delivered@resend.dev'], 12 | subject: "Hello world", 13 | react: EmailTemplate({ firstName: "John" }) as React.ReactElement, 14 | }); 15 | 16 | if (error) { 17 | return Response.json({ error }, { status: 500 }); 18 | } 19 | 20 | return Response.json({ data }); 21 | } catch (error) { 22 | return Response.json({ error }, { status: 500 }); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /app/layout.tsx: -------------------------------------------------------------------------------- 1 | export const metadata = { 2 | title: 'Next.js', 3 | description: 'Generated by Next.js', 4 | } 5 | 6 | export default function RootLayout({ 7 | children, 8 | }: { 9 | children: React.ReactNode 10 | }) { 11 | return ( 12 | 13 | {children} 14 | 15 | ) 16 | } 17 | -------------------------------------------------------------------------------- /app/page.tsx: -------------------------------------------------------------------------------- 1 | export default function Page() { 2 | return

Hello, Home page!

3 | } -------------------------------------------------------------------------------- /components/email-template.tsx: -------------------------------------------------------------------------------- 1 | import * as React from "react"; 2 | 3 | interface EmailTemplateProps { 4 | firstName: string; 5 | } 6 | 7 | export const EmailTemplate: React.FC> = ({ 8 | firstName, 9 | }) => ( 10 |
11 |

Welcome, {firstName}!

12 |
13 | ); 14 | 15 | export default EmailTemplate; 16 | -------------------------------------------------------------------------------- /next-env.d.ts: -------------------------------------------------------------------------------- 1 | /// 2 | /// 3 | 4 | // NOTE: This file should not be edited 5 | // see https://nextjs.org/docs/basic-features/typescript for more information. 6 | -------------------------------------------------------------------------------- /next.config.js: -------------------------------------------------------------------------------- 1 | function throwError(envVar) { 2 | throw `Abort: You need to define ${envVar} in the .env file.` 3 | } 4 | 5 | if (!process.env.RESEND_API_KEY) return throwError('RESEND_API_KEY'); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "resend-nextjs-app-router-example", 3 | "private": true, 4 | "scripts": { 5 | "dev": "next dev" 6 | }, 7 | "dependencies": { 8 | "next": "latest", 9 | "react": "latest", 10 | "react-dom": "latest", 11 | "resend": "latest" 12 | }, 13 | "devDependencies": { 14 | "@types/node": "latest", 15 | "@types/react": "latest", 16 | "typescript": "latest" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Resend with Next.js (App Router) 2 | 3 | This example shows how to use Resend with [Next.js](https://nextjs.org). 4 | 5 | ## Deploy your own 6 | 7 | Deploy the example using [Vercel](https://vercel.com): 8 | 9 | [![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https://github.com/resend/resend-nextjs-app-router-example&project-name=resend-nextjs-app-router-example&repository-name=resend-nextjs-app-router-example&env=RESEND_API_KEY) 10 | 11 | ## Instructions 12 | 13 | 1. Define environment variables in `.env` file. 14 | 15 | ```sh 16 | cp .env.example .env 17 | ``` 18 | 19 | 2. Install dependencies: 20 | 21 | ```sh 22 | npm install 23 | # or 24 | yarn 25 | ``` 26 | 27 | 3. Run Next.js locally: 28 | 29 | ```sh 30 | npm run dev 31 | ``` 32 | 33 | 4. Make a curl request 34 | 35 | ```sh 36 | curl -X POST http://localhost:3000/api/send 37 | ``` 38 | 39 | ## License 40 | 41 | MIT License 42 | -------------------------------------------------------------------------------- /renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://docs.renovatebot.com/renovate-schema.json", 3 | "extends": [ 4 | "config:base" 5 | ] 6 | } 7 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "lib": [ 5 | "dom", 6 | "dom.iterable", 7 | "esnext" 8 | ], 9 | "allowJs": true, 10 | "skipLibCheck": true, 11 | "strict": false, 12 | "forceConsistentCasingInFileNames": true, 13 | "noEmit": true, 14 | "incremental": true, 15 | "esModuleInterop": true, 16 | "module": "esnext", 17 | "resolveJsonModule": true, 18 | "moduleResolution": "node", 19 | "isolatedModules": true, 20 | "jsx": "preserve", 21 | "plugins": [ 22 | { 23 | "name": "next" 24 | } 25 | ] 26 | }, 27 | "include": [ 28 | "next-env.d.ts", 29 | "**/*.ts", 30 | "**/*.tsx", 31 | ".next/types/**/*.ts" 32 | ], 33 | "exclude": [ 34 | "node_modules" 35 | ] 36 | } 37 | --------------------------------------------------------------------------------