Skip to content
Cloudflare Docs
We recommend using Cloudflare Workers for new projects. For existing Pages projects, see our migration guide and compatibility matrix.

Once you have set up next-on-pages, you can access bindings from any route of your Next.js app via getRequestContext:

import { getRequestContext } from "@cloudflare/next-on-pages";
export const runtime = "edge";
export async function GET(request) {
let responseText = "Hello World";
const myKv = getRequestContext().env.MY_KV_NAMESPACE;
await myKv.put("foo", "bar");
const foo = await myKv.get("foo");
return new Response(foo);
}

Add bindings to your Pages project by adding them to your Wrangler configuration file.

TypeScript type declarations for bindings

To ensure that the env object from getRequestContext().env above has accurate TypeScript types, make sure you have generated types by running wrangler types and followed the setup steps.

Other Cloudflare APIs (cf, ctx)

Access context about the incoming request from the cf object, as well as lifecycle methods from the ctx object from the return value of getRequestContext():

import { getRequestContext } from "@cloudflare/next-on-pages";
export const runtime = "edge";
export async function GET(request) {
const { env, cf, ctx } = getRequestContext();
// ...
}