Calling Neurons
Learn how to call Neurons using the REST API or the TypeScript SDK.
Neurons can be called using either the REST API directly or our official TypeScript SDK. This guide explains both methods and their options.
REST API
Each Neuron has a unique API URL in the format:
Authentication Requirements
Authentication is only required if enforced by the Neuron’s access control settings. A Neuron can be configured with:
- Public Access: No authentication required
- Referer Restrictions: Only requests from specific domains are allowed
- IP Restrictions: Only requests from specific IP addresses are allowed
- API Key Authentication: Requires a Prompteus API key
- JWT Authentication: Requires a valid JWT token
When authentication is required, provide it using the Authorization
header:
If your Neuron has public access enabled, you can skip the authentication step entirely. See the Access Control documentation for detailed configuration options.
Basic Usage
To call a Neuron, send a POST request to its API URL with your input in JSON format:
Complete Examples
Here are complete examples showing how to call Neurons with error handling and authentication:
Query Parameters
The API supports the following query parameters:
Parameter | Type | Description |
---|---|---|
bypassCache | boolean | When true, forces a new execution and bypasses both exact and semantic caching |
rawOutput | boolean | When true, returns the raw text output without any processing |
Example with query parameters:
Authentication
If your Neuron requires authentication, you can provide it using the Authorization
header:
See Access Control for more details about authentication methods.
Response Format
A successful response will have this structure:
Error Handling
Errors are returned with appropriate HTTP status codes and a JSON response:
The API uses standard HTTP status codes to indicate the success or failure of requests:
Status Code | Description | Common Causes |
---|---|---|
400 Bad Request | Invalid request parameters | • Malformed input JSON • Missing required fields |
401 Unauthorized | Authentication failed | • Invalid/expired token • Malformed API key • Missing authentication |
403 Forbidden | Access denied | • Access control restrictions • IP not in allowed list • Domain not in allowed referers |
404 Not Found | Resource not found | • Neuron not found • Deployment not found • Organization not found • Invalid neuron configuration |
422 Unprocessable Entity | Validation failed | • Input validation failed • Invalid parameter format |
429 Too Many Requests | Rate limit exceeded | • Rate limits exceeded • Account paused (billing) • Usage limits exceeded |
500 Internal Server Error | Server-side error | • Unexpected server errors • Missing deployment • Configuration issues |
503 Service Unavailable | Service down | • Maintenance mode • System overload • Upstream service failures |
For 429
responses, check the Retry-After
header for the number of seconds to wait before retrying:
Error Handling Examples
Here’s how to handle errors in different languages:
For production applications, we recommend implementing proper retry logic with exponential backoff, especially for 429
and 503
errors. See Rate Limiting for more details about handling rate limits.
TypeScript SDK
We provide an official TypeScript SDK for a more convenient integration experience.
Installation
Authentication
The SDK supports both authenticated and unauthenticated calls, depending on your Neuron’s access control settings. Authentication is only required if your Neuron is configured to use API Key or JWT authentication.
If authentication is needed, you can provide it in multiple ways:
- During client creation:
- Using the setter method:
- Per request:
If your Neuron has public access enabled, you can omit the authentication token. The SDK will work without authentication for public Neurons.
Basic Usage
Complete Example
Here’s a comprehensive example showing different ways to use the SDK:
Advanced Options
The callNeuron
method accepts these options:
Example with options:
Type Safety
The SDK provides TypeScript types for all responses:
Error Handling
The SDK provides built-in error handling with TypeScript types, and exposes the error status code and message from the API. Here’s how to handle different error scenarios:
The SDK throws typed errors that include both the error message and status code, making it easy to implement specific error handling logic for different scenarios.
Complete Example
Here’s a comprehensive example showing different ways to use the SDK:
Related Resources
- Introduction to Neurons - Learn about Neurons
- Access Control - Configure authentication
- Rate Limiting - Understand usage limits
- Caching - Learn about response caching
- Execution Logs - Monitor Neuron calls