How to Add LLMs.txt to Next.js
There is more than one way to add llms.txt to Next.js, and the right one depends on how your site is built. You can drop a plain text file into the public folder, generate it from a route handler in the App Router, or serve it from an API route in the Pages Router with a rewrite. This guide walks through all three, then compares them so you can pick.
Published: [FILL IN] · Last modified: [FILL IN]
Where llms.txt has to live
Whatever method you choose, the file must be reachable at /llms.txt at your domain root. An AI tool looks for that exact path, so a file under a nested route will not be found unless you rewrite it.
llms.txt is a community proposal, not a W3C or IETF standard, and it is not a Google ranking factor. Treat it as an emerging convention: the setup below is about making your site easy for AI tools to read, not about ranking.
Method 1 — a static file in /public
Next.js serves everything in /public from the site root, so a file at public/llms.txt is automatically available at /llms.txt. No code, no build step.
# Acme Docs
> Guides and API references for the Acme platform.
## Docs
- [Getting started](https://acme.com/docs/getting-started): Install and run in minutes.
- [API reference](https://acme.com/docs/api): Every endpoint and parameter.
## Guides
- [Best practices](https://acme.com/docs/best-practices): Patterns we recommend.
## Optional
- [llms-full.txt](https://acme.com/llms-full.txt): Full page text in one file.This works in both the App Router and the Pages Router, and it also works for a static export. The trade-off is that the file is frozen at build time: if your pages change, you edit and redeploy the file by hand.
Method 2 — an App Router route handler
If you would rather generate the file than hand-maintain it, add a route handler at app/llms.txt/route.ts. Next.js maps that folder name to the /llms.txt path and serves whatever the handler returns. Set the content type to plain text so the response is read as Markdown.
import { NextResponse } from "next/server";
const LLMS_TXT = `# Acme Docs
> Guides and API references for the Acme platform.
## Docs
- [Getting started](https://acme.com/docs/getting-started): Install and run in minutes.
- [API reference](https://acme.com/docs/api): Every endpoint and parameter.
## Optional
- [llms-full.txt](https://acme.com/llms-full.txt): Full page text in one file.
`;
export async function GET() {
return new NextResponse(LLMS_TXT, {
headers: { "content-type": "text/plain; charset=utf-8" },
});
}Because this runs on the server, you can build the string from anywhere: a CMS, a database query, or a list of routes read from your content layer. Keep it simple and return a plain Response; there is no need for segment config options here.
If the source data changes rarely, cache the upstream fetch and give it a revalidate window (for example { next: { revalidate: 3600 } }) so the file is rebuilt hourly instead of on every request. That keeps the handler fast without turning it fully static.
Method 3 — a Pages Router API route with a rewrite
On the Pages Router there is no folder-to-path mapping for text files, so you serve the content from an API route and point /llms.txt at it with a rewrite. First create the API route.
import type { NextApiRequest, NextApiResponse } from "next";
const LLMS_TXT = `# Acme Docs
> Guides and API references for the Acme platform.
## Docs
- [Getting started](https://acme.com/docs/getting-started): Install and run in minutes.
- [API reference](https://acme.com/docs/api): Every endpoint and parameter.
`;
export default function handler(_req: NextApiRequest, res: NextApiResponse) {
res.setHeader("content-type", "text/plain; charset=utf-8");
res.send(LLMS_TXT);
}Then add a rewrite in next.config.js so the root path resolves to that route.
/** @type {import('next').NextConfig} */
const nextConfig = {
async rewrites() {
return [{ source: "/llms.txt", destination: "/api/llms-txt" }];
},
};
module.exports = nextConfig;The rewrite keeps the public URL clean at /llms.txt while your implementation lives at /api/llms-txt. As with the App Router handler, you can assemble the string from a CMS or any server-side data source.
Which method should you use?
All three end with the same result: a file at your domain root. They differ in how much setup they need and whether the content can change without a deploy.
| Static file in /public | App Router route handler | Pages Router API + rewrite | |
|---|---|---|---|
| Ease | Easiest — add one file | A few lines of TypeScript | Most setup — API route plus a rewrite |
| Dynamic or static | Static only | Can build at request time | Can build at request time |
| Best for | Most sites, including static exports | App Router sites that want one source of truth | Existing Pages Router projects |
Start with the static file unless you truly need the content generated at runtime. It is the least code to own, and you can always move to a route handler later without changing the public URL.
Next step — V2 in Next.js
Once the file is live, the V2 revision adds subfolder files and cleaner Markdown handling. The llms.txt V2 on Next.js guide covers how to structure folders and link Markdown versions of your pages.
Check your file
Generate yours free
Learn more
- What is llms.txt?
- llms.txt V2: what changed
- Format & specification
- How to create llms.txt
- Best practices
- Examples & templates
- Do I need llms.txt?
- llms.txt for SEO
- What is llms-full.txt?
- llms.txt vs llms-full.txt
- llms.txt vs robots.txt
- llms.txt in the news
- LLMs.txt Generator
- LLMs-full.txt Generator
- LLMs.txt Validator
- llms.txt V2 Checker
- Robots.txt Generator
- Robots.txt Tester