Set up the inference shell proxy in Next.js with App Router or Page Router.
Installation
1npm install @inferencesh/sdkApp Router (Recommended)
Create app/api/inference/proxy/route.ts:
1import { createHandler } from "@inferencesh/sdk/proxy/nextjs";23export const { GET, POST } = createHandler({4 allowedEndpoints: ["my-team/*"],5});The proxy forwards app runs and agent chats only; allowedEndpoints limits them to your apps and agents. See options.
[!WARNING] Anyone who can reach the proxy route runs apps and agents on your API key. In production set
allowedEndpointsandisAuthenticated(see what the proxy forwards), and create a Run only key in a workspace that holds nothing else.
With Edge Runtime
1import { createHandler } from "@inferencesh/sdk/proxy/nextjs";23export const runtime = "edge";4export const { GET, POST } = createHandler({ allowedEndpoints: ["my-team/*"] });Page Router
Create pages/api/inference/proxy.ts:
1import { createPageHandler } from "@inferencesh/sdk/proxy/nextjs";23export default createPageHandler({ allowedEndpoints: ["my-team/*"] });Note: Page Router doesn't support streaming. Use App Router for real-time updates.
Environment Variable
Add your API key to .env.local:
1INFERENCE_API_KEY="inf_your_key_here"Client Configuration
1"use client";23import { inference } from "@inferencesh/sdk";45const client = inference({6 proxyUrl: "/api/inference/proxy"7});89export async function generateImage(prompt: string) {10 const result = await client.run({11 app: "infsh/flux",12 input: { prompt }13 });14 15 return result.output;16}Authentication
isAuthenticated gets the NextRequest; false answers 401:
1// app/api/inference/proxy/route.ts2import { createHandler } from "@inferencesh/sdk/proxy/nextjs";3import { getSession } from "@/lib/auth";45export const { GET, POST } = createHandler({6 allowedEndpoints: ["my-team/*"],7 isAuthenticated: async (req) => Boolean(await getSession(req)),8});CORS (Cross-Origin)
If calling from a different domain:
1import { createHandler } from "@inferencesh/sdk/proxy/nextjs";2import { NextResponse } from "next/server";34export async function OPTIONS() {5 return new NextResponse(null, {6 headers: {7 "Access-Control-Allow-Origin": "*",8 "Access-Control-Allow-Methods": "GET, POST, OPTIONS",9 "Access-Control-Allow-Headers": "Content-Type, x-inf-target-url",10 },11 });12}1314export const { GET, POST } = createHandler({ allowedEndpoints: ["my-team/*"] });Vercel Deployment
See Vercel for deployment-specific configuration and timeout settings.