introduction.md 3.3 KB
Newer Older
1 2 3 4
---
description: Next.js supports API Routes, which allow you to build your API without leaving your Next.js app. Learn how it works here.
---

L
Luis Alvarez D 已提交
5 6
# API Routes

7
<details open>
L
Luis Alvarez D 已提交
8 9
  <summary><b>Examples</b></summary>
  <ul>
10 11 12 13 14
    <li><a href="https://github.com/vercel/next.js/tree/canary/examples/api-routes">Basic API Routes</a></li>
    <li><a href="https://github.com/vercel/next.js/tree/canary/examples/api-routes-middleware">API Routes with middleware</a></li>
    <li><a href="https://github.com/vercel/next.js/tree/canary/examples/api-routes-graphql">API Routes with GraphQL</a></li>
    <li><a href="https://github.com/vercel/next.js/tree/canary/examples/api-routes-rest">API Routes with REST</a></li>
    <li><a href="https://github.com/vercel/next.js/tree/canary/examples/api-routes-cors">API Routes with CORS</a></li>
L
Luis Alvarez D 已提交
15 16 17 18 19
  </ul>
</details>

API routes provide a straightforward solution to build your **API** with Next.js.

L
Luis Alvarez D 已提交
20
Any file inside the folder `pages/api` is mapped to `/api/*` and will be treated as an API endpoint instead of a `page`. They are server-side only bundles and won't increase your client-side bundle size.
L
Luis Alvarez D 已提交
21

22
For example, the following API route `pages/api/user.js` handles a `json` response:
L
Luis Alvarez D 已提交
23 24

```js
25
export default function handler(req, res) {
L
Luis Alvarez D 已提交
26 27
  res.statusCode = 200
  res.setHeader('Content-Type', 'application/json')
28
  res.end(JSON.stringify({ name: 'John Doe' }))
L
Luis Alvarez D 已提交
29 30 31
}
```

32
For an API route to work, you need to export a function as default (a.k.a **request handler**), which then receives the following parameters:
L
Luis Alvarez D 已提交
33 34 35 36 37 38 39

- `req`: An instance of [http.IncomingMessage](https://nodejs.org/api/http.html#http_class_http_incomingmessage), plus some pre-built middlewares you can see [here](/docs/api-routes/api-middlewares.md)
- `res`: An instance of [http.ServerResponse](https://nodejs.org/api/http.html#http_class_http_serverresponse), plus some helper functions you can see [here](/docs/api-routes/response-helpers.md)

To handle different HTTP methods in an API route, you can use `req.method` in your request handler, like so:

```js
40
export default function handler(req, res) {
L
Luis Alvarez D 已提交
41 42 43 44 45 46 47 48
  if (req.method === 'POST') {
    // Process a POST request
  } else {
    // Handle any other HTTP method
  }
}
```

L
Luis Alvarez D 已提交
49 50
To fetch API endpoints, take a look into any of the examples at the start of this section.

L
Luis Alvarez D 已提交
51
## Caveats
L
Luis Alvarez D 已提交
52

L
Luis Alvarez D 已提交
53 54
- API Routes [do not specify CORS headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS), meaning they are **same-origin only** by default. You can customize such behavior by wrapping the request handler with the [cors middleware](/docs/api-routes/api-middlewares.md#connectexpress-middleware-support).
- API Routes can't be used with [`next export`](/docs/advanced-features/static-html-export.md)
L
Luis Alvarez D 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

## Related

For more information on what to do next, we recommend the following sections:

<div class="card">
  <a href="/docs/api-routes/api-middlewares.md">
    <b>API Middlewares:</b>
    <small>learn about the built-in middlewares for the request.</small>
  </a>
</div>

<div class="card">
  <a href="/docs/api-routes/response-helpers.md">
    <b>Response Helpers:</b>
    <small>learn about the built-in methods for the response.</small>
  </a>
</div>

<div class="card">
  <a href="/docs/basic-features/typescript.md#api-routes">
    <b>TypeScript:</b>
77
    <small>Add TypeScript to your API Routes.</small>
L
Luis Alvarez D 已提交
78 79
  </a>
</div>