Express

Set up the inference shell proxy in Express.js applications.


Installation

bash
1npm install @inferencesh/sdk express

Basic Setup

typescript
1import express from "express";2import { createHandler, PROXY_ROUTE } from "@inferencesh/sdk/proxy/express";34const app = express();56// Parse JSON bodies7app.use(express.json());89// Mount the proxy10const proxy = createHandler({11  allowedEndpoints: ["my-team/*"],12  isAuthenticated: async (req) => Boolean(req.session?.user),13});14app.all(PROXY_ROUTE, proxy);1516app.listen(3000, () => {17  console.log("Server running on port 3000");18});

The default route is /api/inference/proxy. The proxy forwards app runs and agent chats only; allowedEndpoints limits them to your apps and agents, and isAuthenticated gets the Express request. 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.


Environment Variable

Set your API key:

bash
1export INFERENCE_API_KEY="inf_your_key_here"

Or use a .env file with dotenv:

bash
1# .env2INFERENCE_API_KEY=inf_your_key_here
typescript
1import "dotenv/config";2import express from "express";3import * as inferenceProxy from "@inferencesh/sdk/proxy/express";45const app = express();6app.use(express.json());7app.all(PROXY_ROUTE, proxy);

Client Configuration

typescript
1import { inference } from "@inferencesh/sdk";23const client = inference({4  proxyUrl: "http://localhost:3000/api/inference/proxy"5});67const result = await client.run({8  app: "infsh/flux",9  input: { prompt: "A sunset" }10});

Custom Route

Mount at a different path:

typescript
1app.all("/my-custom-proxy/*", proxy);

Update client:

typescript
1const client = inference({2  proxyUrl: "/my-custom-proxy"3});

Rate Limiting

typescript
1import rateLimit from "express-rate-limit";23const limiter = rateLimit({4  windowMs: 15 * 60 * 1000, // 15 minutes5  max: 100, // limit each IP to 100 requests per window6});78app.all(PROXY_ROUTE, limiter, proxy);

CORS Support

typescript
1import cors from "cors";23app.use(cors({4  origin: "https://myapp.com",5  methods: ["GET", "POST"],6  allowedHeaders: ["Content-Type", "x-inf-target-url"],7}));89app.all(inferenceProxy.route, inferenceProxy.handler);

Streaming

The Express handler automatically supports streaming responses for real-time task updates.

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.