Inference Logoinference.sh

JavaScript SDK

JavaScript/TypeScript SDK for inference.sh agents and apps.

The JavaScript SDK provides a fully-typed client for running apps and chatting with agents.


Installation

bash
1npm install @inferencesh/sdk

Running Apps

Basic Usage

typescript
1import { inference } from '@inferencesh/sdk';2 3const client = inference({ apiKey: 'inf_your_key' });4 5// Run and wait for completion (default)6const result = await client.run({7  app: 'infsh/echo',8  input: { message: 'Hello from JavaScript!' }9});10 11console.log(result.output);

With Progress Updates

typescript
1const result = await client.run(2  { app: 'infsh/flux-schnell', input: {...} },3  {4    onUpdate: (update) => {5      console.log(`Status: ${update.status}`);6      if (update.logs?.length) {7        console.log(update.logs[update.logs.length - 1]);8      }9    }10  }11);

Fire and Forget

typescript
1const task = await client.run(2  { app: 'infsh/slow-task', input: {...} },3  { wait: false }4);5console.log(`Task ID: ${task.id}`);

Agent SDK

Chat with AI agents programmatically.

Using an Existing Agent

Reference agents created in the workspace:

typescript
1import { inference } from '@inferencesh/sdk';2 3const client = inference({ apiKey: 'inf_your_key' });4 5// Use an agent you created in the workspace6const agent = client.agent('my-org/my-agent@abc123'); // Format: namespace/name@version7 8const response = await agent.sendMessage('Hello!');9console.log(response);

Creating Ad-Hoc Agents

Create agents on-the-fly without saving to workspace:

typescript
1import { inference, tool, string, internalTools } from '@inferencesh/sdk';2 3const client = inference({ apiKey: 'inf_your_key' });4 5// Define tools6const calculator = tool('calculator')7  .describe('Performs math operations')8  .param('expression', string('Math expression'))9  .build();10 11// Create ad-hoc agent12const agent = client.agent({13  core_app_ref: 'infsh/claude-sonnet-4@latest',14  name: 'Math Assistant',15  system_prompt: 'You help with math. Use the calculator tool.',16  tools: [calculator],17  internal_tools: internalTools().memory().build(),18});19 20const response = await agent.sendMessage('What is 42 * 17?');

Streaming Responses

typescript
1await agent.sendMessage('Explain quantum computing', {2  onMessage: (msg) => {3    if (msg.content) {4      for (const c of msg.content) {5        if (c.type === 'text' && c.text) {6          process.stdout.write(c.text);7        }8      }9    }10  },11  onToolCall: async (call) => {12    console.log(`\n[Tool: ${call.name}]`);13    // Execute tool and return result string14    const result = await executeMyTool(call.name, call.args);15    await agent.submitToolResult(call.id, result);16    17    // For widget interactions, you can also pass action + form_data:18    // await agent.submitToolResult(call.id, {19    //   action: { type: 'confirm' },20    //   form_data: { name: 'John', email: '[email protected]' }21    // });22  },23});

File Attachments

typescript
1// From Blob2const imageBlob = new Blob([imageBuffer], { type: 'image/png' });3const response = await agent.sendMessage('What\'s in this image?', {4  files: [imageBlob]5});6 7// From data URI8const response = await agent.sendMessage('Analyze this', {9  files: ['data:image/png;base64,iVBORw0KGgo...']10});

Chat Management

typescript
1// Get current chat2const chat = await agent.getChat();3console.log(`Chat ID: ${chat?.id}`);4console.log(`Messages: ${chat?.messages.length}`);5 6// Stop generation7await agent.stopChat();8 9// Start fresh10agent.reset();11 12// Cleanup streams13agent.disconnect();

Tool Builder

Define tools with the fluent API:

typescript
1import {2  tool, appTool, agentTool, webhookTool, internalTools,3  string, number, integer, boolean, enumOf, array, object, optional4} from '@inferencesh/sdk';5 6// Client tool (runs in your code)7const search = tool('search_files')8  .describe('Search for files')9  .param('pattern', string('Glob pattern'))10  .param('recursive', optional(boolean('Search subdirs')))11  .build();12 13// App tool (calls another inference app)14const generate = appTool('generate', 'infsh/flux-schnell@latest')15  .describe('Generate an image')16  .param('prompt', string('Image description'))17  .build();18 19// Agent tool (delegates to sub-agent)20const researcher = agentTool('research', 'my-org/researcher@v1')21  .describe('Research a topic')22  .param('topic', string('Topic'))23  .build();24 25// Webhook tool (calls external API)26const notify = webhookTool('slack', 'https://hooks.slack.com/...')27  .describe('Send Slack message')28  .secret('SLACK_SECRET')29  .param('message', string('Message'))30  .build();31 32// Internal tools config33const internals = internalTools().memory().plan().build();

Full Tool Builder Reference


TypeScript Types

The SDK is fully typed:

typescript
1import type {2  TaskDTO,3  ChatDTO,4  ChatMessageDTO,5  AgentTool,6  TaskStatusCompleted,7  TaskStatusFailed,8} from '@inferencesh/sdk';9 10// Check task status11if (result.status === TaskStatusCompleted) {12  console.log('Done!');13} else if (result.status === TaskStatusFailed) {14  console.log('Failed:', result.error);15}

CommonJS Support

javascript
1const { inference, tool, string } = require('@inferencesh/sdk');2 3const client = inference({ apiKey: 'inf_...' });4const result = await client.run({...});

Error Handling

typescript
1import { RequirementsNotMetException, InferenceError } from '@inferencesh/sdk';2 3try {4  const result = await client.run({ app: 'my-app', input: {...} });5} catch (e) {6  if (e instanceof RequirementsNotMetException) {7    console.log('Missing requirements:');8    for (const err of e.errors) {9      console.log(`  - ${err.type}: ${err.key}`);10    }11  } else if (e instanceof InferenceError) {12    console.log('API error:', e.message);13  }14}

File Upload

typescript
1// Upload separately2const file = await client.uploadFile(buffer, {3  filename: 'image.png',4  contentType: 'image/png'5});6console.log(`Uploaded: ${file.uri}`);7 8// Use in app input9const result = await client.run({10  app: 'image-processor',11  input: { image: file.uri }12});

REST API (curl)

For simple integrations, you can use the REST API directly with streaming:

bash
1curl -N -X POST \2  -H "Authorization: Bearer YOUR_API_KEY" \3  -H "Content-Type: application/json" \4  -d '{5    "agent": "my-org/assistant@abc123",6    "input": {"text": "Hello!", "role": "user"},7    "stream": true8  }' \9  https://api.inference.sh/agents/run

The -N flag disables curl's buffering so you see events in real-time.

Without streaming (returns JSON with chat_id for subsequent messages):

bash
1curl -X POST \2  -H "Authorization: Bearer YOUR_API_KEY" \3  -H "Content-Type: application/json" \4  -d '{5    "agent": "my-org/assistant@abc123",6    "input": {"text": "Hello!", "role": "user"}7  }' \8  https://api.inference.sh/agents/run

What's Next?

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.