Variable: webapi
@providerprotocol/ai / proxy / webapi
Variable: webapi
Section titled “Variable: webapi”
constwebapi:object
Defined in: src/providers/proxy/server/webapi.ts:451
Web API adapter utilities.
For use with Bun, Deno, Next.js App Router, Cloudflare Workers, and other frameworks that support Web API Response.
Security Note: The proxy works without configuration, meaning no authentication by default. Always add your own auth layer in production.
Type Declaration
Section titled “Type Declaration”bindTools()
Section titled “bindTools()”bindTools: (
schemas,implementations) =>Tool<unknown,unknown>[]
Bind tool schemas to implementation functions.
Takes tool schemas from the request and binds them to your server-side implementations.
Parameters
Section titled “Parameters”schemas
Section titled “schemas”Tool schemas from the request
object[] | undefined
implementations
Section titled “implementations”Record<string, (params) => unknown | Promise<unknown>>
Map of tool name to implementation
Returns
Section titled “Returns”Tool<unknown, unknown>[]
Array of complete Tool objects
Example
Section titled “Example”const { tools: schemas } = parseBody(body);
const tools = bindTools(schemas, { get_weather: async ({ location }) => fetchWeather(location), search: async ({ query }) => searchDB(query),});
const instance = llm({ model, tools });parseBody()
Section titled “parseBody()”parseBody: (
body) =>ParsedRequest
Parse an HTTP request body into PP types.
Parameters
Section titled “Parameters”unknown
The JSON-parsed request body
Returns
Section titled “Returns”Deserialized PP data
Example
Section titled “Example”const body = await req.json();const { messages, system, params } = parseBody(body);
const instance = llm({ model: anthropic('...'), system, params });const turn = await instance.generate(messages);parseEmbeddingBody()
Section titled “parseEmbeddingBody()”parseEmbeddingBody: (
body) =>ParsedEmbeddingRequest
Parse an HTTP request body into embedding inputs.
Parameters
Section titled “Parameters”unknown
The JSON-parsed request body
Returns
Section titled “Returns”Parsed embedding request data
parseImageBody()
Section titled “parseImageBody()”parseImageBody: (
body) =>ParsedImageRequest
Parse an HTTP request body into image request data.
Parameters
Section titled “Parameters”unknown
The JSON-parsed request body
Returns
Section titled “Returns”Parsed image request data
toEmbeddingJSON()
Section titled “toEmbeddingJSON()”toEmbeddingJSON: (
result) =>Response
Create a JSON Response from an embedding result.
Parameters
Section titled “Parameters”result
Section titled “result”The embedding result
Returns
Section titled “Returns”Response
HTTP Response with JSON body
toError()
Section titled “toError()”toError: (
message,status) =>Response
Create an error Response.
Parameters
Section titled “Parameters”message
Section titled “message”string
Error message
status
Section titled “status”number = 500
HTTP status code (default: 500)
Returns
Section titled “Returns”Response
HTTP Response with error body
toImageJSON()
Section titled “toImageJSON()”toImageJSON: (
result) =>Response
Create a JSON Response from an image result.
Parameters
Section titled “Parameters”result
Section titled “result”The image result
Returns
Section titled “Returns”Response
HTTP Response with JSON body
toImageSSE()
Section titled “toImageSSE()”toImageSSE: (
stream) =>Response
Create an SSE Response from an ImageStreamResult.
Streams image events as SSE, then sends the final image result.
Parameters
Section titled “Parameters”stream
Section titled “stream”ImageStreamLike
The ImageStreamResult or ImageProviderStreamResult from image().stream()
Returns
Section titled “Returns”Response
HTTP Response with SSE body
toJSON()
Section titled “toJSON()”toJSON: (
turn) =>Response
Create a JSON Response from a Turn.
Parameters
Section titled “Parameters”The completed inference turn
Returns
Section titled “Returns”Response
HTTP Response with JSON body
Example
Section titled “Example”const turn = await instance.generate(messages);return toJSON(turn);toSSE()
Section titled “toSSE()”toSSE: (
stream) =>Response
Create an SSE Response from a StreamResult.
Streams PP StreamEvents as SSE, then sends the final Turn data.
Parameters
Section titled “Parameters”stream
Section titled “stream”The StreamResult from instance.stream()
Returns
Section titled “Returns”Response
HTTP Response with SSE body
Example
Section titled “Example”const stream = instance.stream(messages);return toSSE(stream);Examples
Section titled “Examples”import { llm } from '@providerprotocol/ai';import { anthropic } from '@providerprotocol/ai/anthropic';import { parseBody, toJSON, toSSE } from '@providerprotocol/ai/proxy';
// Bun.serve / Deno.serve / Next.js App Routerexport async function POST(req: Request) { const { messages, system } = parseBody(await req.json()); const instance = llm({ model: anthropic('claude-sonnet-4-20250514'), system });
if (req.headers.get('accept')?.includes('text/event-stream')) { return toSSE(instance.stream(messages)); } return toJSON(await instance.generate(messages));}import { llm, exponentialBackoff, roundRobinKeys } from '@providerprotocol/ai';import { anthropic } from '@providerprotocol/ai/anthropic';import { parseBody, toJSON, toSSE, toError } from '@providerprotocol/ai/proxy';
// Your platform's user validationasync function validateToken(token: string): Promise<{ id: string } | null> { // Verify JWT, check database, etc. return token ? { id: 'user-123' } : null;}
// Server manages AI provider keys - users never see themconst claude = llm({ model: anthropic('claude-sonnet-4-20250514'), config: { apiKey: roundRobinKeys([process.env.ANTHROPIC_KEY_1!, process.env.ANTHROPIC_KEY_2!]), retryStrategy: exponentialBackoff({ maxAttempts: 3 }), },});
Bun.serve({ port: 3000, async fetch(req) { // Authenticate with YOUR platform credentials const token = req.headers.get('Authorization')?.replace('Bearer ', ''); const user = await validateToken(token ?? ''); if (!user) return toError('Unauthorized', 401);
// Rate limit, track usage, bill user, etc. // await trackUsage(user.id);
const { messages, system, params } = parseBody(await req.json());
if (params?.stream) { return toSSE(claude.stream(messages, { system })); } return toJSON(await claude.generate(messages, { system })); },});