@stagewise/api-client
v2.1.0
Published
Public Node.js client for Stagewise API
Readme
@stagewise/api-client
A type-safe Node.js client for the Stagewise API, built with tRPC.
Installation
npm install @stagewise/api-client
# or
pnpm add @stagewise/api-client
# or
yarn add @stagewise/api-clientUsage
Basic Usage
import { createNodeApiClient } from '@stagewise/api-client';
const client = createNodeApiClient({
baseUrl: 'https://v1.api.stagewise.io', // optional, defaults to this
headers: {
'x-access-key': 'your-api-key', // optional if using environment variable
}
});
// Health check
const health = await client.health.query();
console.log(health);Using Default Client
import { nodeApiClient } from '@stagewise/api-client';
// Uses default configuration
const result = await nodeApiClient.health.query();Configuration
The client can be configured in several ways:
Environment Variables
API_URL=https://v1.api.stagewise.io
API_ACCESS_KEY=your-api-keyConstructor Options
import { createNodeApiClient } from '@stagewise/api-client';
const client = createNodeApiClient({
baseUrl: 'http://localhost:3001', // Custom API URL
headers: {
'x-access-key': 'your-api-key',
'custom-header': 'value'
},
fetch: customFetch // Custom fetch implementation
});Agent Streaming
import { createNodeApiClient } from '@stagewise/api-client';
const client = createNodeApiClient();
const response = await client.agent.streamText.mutate({
messages: [{ role: 'user', content: 'Hello!' }],
model: 'gemini-2.0-flash-lite',
tools: {}
});
// Handle streaming response
for await (const chunk of response) {
console.log(chunk);
}Types
All types are automatically inferred from the API router:
import type {
AppRouter,
RouterInputs,
RouterOutputs,
TRPCClient
} from '@stagewise/api-client';
// Input types for API calls
type HealthInput = RouterInputs['health'];
// Output types for API responses
type HealthOutput = RouterOutputs['health'];
// Full client type
type Client = TRPCClient<AppRouter>;Authentication
The client uses API keys for authentication. Set your API key either:
- As an environment variable:
API_ACCESS_KEY=your-key - In the client options:
headers: { 'x-access-key': 'your-key' }
Error Handling
import { TRPCError } from '@trpc/client';
try {
const result = await client.someEndpoint.query();
} catch (error) {
if (error instanceof TRPCError) {
console.error('tRPC Error:', error.code, error.message);
} else {
console.error('Network Error:', error);
}
}License
MIT
