Inference Logoinference.sh

Next.js

Set up the inference.sh 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 { route } from "@inferencesh/sdk/proxy/nextjs";23export const { GET, POST, PUT } = route;

That's it! The route handler exports all necessary HTTP methods.

With Edge Runtime

typescript
1import { route } from "@inferencesh/sdk/proxy/nextjs";23export const runtime = "edge";4export const { GET, POST, PUT } = route;

Page Router

Create pages/api/inference/proxy.ts:

typescript
1export { handler as default } from "@inferencesh/sdk/proxy/nextjs";

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}

Custom Logic

Add authentication, rate limiting, or analytics:

typescript
1// app/api/inference/proxy/route.ts2import { route } from "@inferencesh/sdk/proxy/nextjs";3import { getSession } from "@/lib/auth";45export const POST = async (req: Request) => {6  // Check authentication7  const session = await getSession();8  if (!session) {9    return Response.json({ error: "Unauthorized" }, { status: 401 });10  }1112  // Add analytics13  await analytics.track("inference_request", {14    userId: session.user.id,15  });1617  // Forward to inference.sh18  return route.POST(req);19};2021export const GET = route.GET;22export const PUT = route.PUT;

CORS (Cross-Origin)

If calling from a different domain:

typescript
1import { route } 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, PUT, OPTIONS",9      "Access-Control-Allow-Headers": "Content-Type, x-inf-target-url",10    },11  });12}1314export const { GET, POST, PUT } = route;

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.