Bindings
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.
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.
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();
// ...}
Was this helpful?
- Resources
- API
- New to Cloudflare?
- Products
- Sponsorships
- Open Source
- Support
- Help Center
- System Status
- Compliance
- GDPR
- Company
- cloudflare.com
- Our team
- Careers
- 2025 Cloudflare, Inc.
- Privacy Policy
- Terms of Use
- Report Security Issues
- Trademark