Next.js

Set up the inference shell proxy in Next.js with App Router or Page Router.


Installation

bash
1npm install @inferencesh/sdk

Create app/api/inference/proxy/route.ts:

typescript
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 allowedEndpoints and isAuthenticated (see what the proxy forwards), and create a Run only key in a workspace that holds nothing else.

With Edge Runtime

typescript
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:

typescript
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:

bash
1INFERENCE_API_KEY="inf_your_key_here"

Client Configuration

typescript
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:

typescript
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:

typescript
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.

we use cookies

we use cookies to ensure you get the best experience on our website. for more information on how we use cookies, please see our cookie policy.

by clicking "accept", you agree to our use of cookies.
learn more.