Skip to content

Variable: webapi

@providerprotocol/ai


@providerprotocol/ai / proxy / webapi

const webapi: 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.

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.

Tool schemas from the request

object[] | undefined

Record<string, (params) => unknown | Promise<unknown>>

Map of tool name to implementation

Tool<unknown, unknown>[]

Array of complete Tool objects

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: (body) => ParsedRequest

Parse an HTTP request body into PP types.

unknown

The JSON-parsed request body

ParsedRequest

Deserialized PP data

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: (body) => ParsedEmbeddingRequest

Parse an HTTP request body into embedding inputs.

unknown

The JSON-parsed request body

ParsedEmbeddingRequest

Parsed embedding request data

parseImageBody: (body) => ParsedImageRequest

Parse an HTTP request body into image request data.

unknown

The JSON-parsed request body

ParsedImageRequest

Parsed image request data

toEmbeddingJSON: (result) => Response

Create a JSON Response from an embedding result.

EmbeddingResult

The embedding result

Response

HTTP Response with JSON body

toError: (message, status) => Response

Create an error Response.

string

Error message

number = 500

HTTP status code (default: 500)

Response

HTTP Response with error body

toImageJSON: (result) => Response

Create a JSON Response from an image result.

ImageResult

The image result

Response

HTTP Response with JSON body

toImageSSE: (stream) => Response

Create an SSE Response from an ImageStreamResult.

Streams image events as SSE, then sends the final image result.

ImageStreamLike

The ImageStreamResult or ImageProviderStreamResult from image().stream()

Response

HTTP Response with SSE body

toJSON: (turn) => Response

Create a JSON Response from a Turn.

Turn

The completed inference turn

Response

HTTP Response with JSON body

const turn = await instance.generate(messages);
return toJSON(turn);

toSSE: (stream) => Response

Create an SSE Response from a StreamResult.

Streams PP StreamEvents as SSE, then sends the final Turn data.

StreamResult

The StreamResult from instance.stream()

Response

HTTP Response with SSE body

const stream = instance.stream(messages);
return toSSE(stream);
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 Router
export 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 validation
async 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 them
const 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 }));
},
});