| 1 | # Route Handlers |
| 2 | |
| 3 | Create API endpoints with `route.ts` files. |
| 4 | |
| 5 | ## Basic Usage |
| 6 | |
| 7 | ```tsx |
| 8 | // app/api/users/route.ts |
| 9 | export async function GET |
| 10 | const users = |
| 11 | return Response.json |
| 12 | } |
| 13 | |
| 14 | export async function POST |
| 15 | const body = |
| 16 | const user = |
| 17 | return Response.json |
| 18 | } |
| 19 | ``` |
| 20 | |
| 21 | ## Supported Methods |
| 22 | |
| 23 | `GET`, `POST`, `PUT |
| 24 | |
| 25 | ## GET Handler Conflicts with page.tsx |
| 26 | |
| 27 | **A `route.ts |
| 28 | |
| 29 | ``` |
| 30 | app/ |
| 31 | ├── api/ |
| 32 | │ └── users/ |
| 33 | │ └── route.ts # /api/users |
| 34 | └── users/ |
| 35 | ├── page.tsx # /users (page) |
| 36 | └── route.ts # Warning: Conflicts with page.tsx! |
| 37 | ``` |
| 38 | |
| 39 | If you need both a page and an API at the same path, use different paths: |
| 40 | |
| 41 | ``` |
| 42 | app/ |
| 43 | ├── users/ |
| 44 | │ └── page.tsx # /users (page) |
| 45 | └── api/ |
| 46 | └── users/ |
| 47 | └── route.ts # /api/users (API) |
| 48 | ``` |
| 49 | |
| 50 | ## Environment Behavior |
| 51 | |
| 52 | Route handlers run in a **Server Component-like environment**: |
| 53 | |
| 54 | - Yes: Can use `async/await` |
| 55 | - Yes: Can access `cookies()`, `headers()` |
| 56 | - Yes: Can use Node.js APIs |
| 57 | - No: Cannot use React hooks |
| 58 | - No: Cannot use React DOM APIs |
| 59 | - No: Cannot use browser APIs |
| 60 | |
| 61 | ```tsx |
| 62 | // Bad: This won't work - no React DOM in route handlers |
| 63 | import { renderToString } |
| 64 | |
| 65 | export async function GET |
| 66 | const html = |
| 67 | return new Response |
| 68 | } |
| 69 | ``` |
| 70 | |
| 71 | ## Dynamic Route Handlers |
| 72 | |
| 73 | ```tsx |
| 74 | // app/api/users/[id]/route.ts |
| 75 | export async function GET |
| 76 | const { id |
| 77 | const user = |
| 78 | |
| 79 | if (!user) |
| 80 | return Response.json |
| 81 | } |
| 82 | |
| 83 | return Response.json |
| 84 | } |
| 85 | ``` |
| 86 | |
| 87 | ## Request Helpers |
| 88 | |
| 89 | ```tsx |
| 90 | export async function GET |
| 91 | // URL and search params |
| 92 | const { searchParams |
| 93 | const query = |
| 94 | |
| 95 | // Headers |
| 96 | const authHeader = |
| 97 | |
| 98 | // Cookies (Next.js helper) |
| 99 | const cookieStore = |
| 100 | const token = |
| 101 | |
| 102 | return Response.json |
| 103 | } |
| 104 | ``` |
| 105 | |
| 106 | ## Response Helpers |
| 107 | |
| 108 | ```tsx |
| 109 | // JSON response |
| 110 | return Response.json( |
| 111 | |
| 112 | // With status |
| 113 | return Response.json( |
| 114 | |
| 115 | // With headers |
| 116 | return Response.json( |
| 117 | headers: { |
| 118 | "Cache-Control": "max-age=3600", |
| 119 | }, |
| 120 | }); |
| 121 | |
| 122 | // Redirect |
| 123 | return Response.redirect( |
| 124 | |
| 125 | // Stream |
| 126 | return new Response( |
| 127 | headers: { "Content-Type": |
| 128 | }); |
| 129 | ``` |
| 130 | |
| 131 | ## When to Use Route Handlers vs Server Actions |
| 132 | |
| 133 | | Use Case | Route Handlers | Server Actions | |
| 134 | | ------------------------ | -------------- | |
| 135 | | Form submissions | No | Yes | |
| 136 | | Data mutations from UI | No | Yes | |
| 137 | | Third-party webhooks | Yes | No | |
| 138 | | External API consumption | Yes | No | |
| 139 | | Public REST API | Yes | No | |
| 140 | | File uploads | Both work | Both work | |
| 141 | |
| 142 | **Prefer Server Actions** for mutations triggered from your UI. |
| 143 | **Use Route Handlers** for external integrations and public APIs. |