Node.js Integration Guide
Add AI safety guardrails to your Node.js applications. This guide covers fetch, axios, middleware patterns, and Express integration.
Prerequisites
- Node.js 18+ (for native fetch) or Node.js 14+ with axios
- A Tork API key (get one here)
- Basic knowledge of async/await
Environment Setup
Store your API key securely using environment variables.
Never commit your API key to version control. Add .env to your .gitignore.
Basic Usage
Make your first API call using fetch or axios.
Reusable Client
Create a wrapper function for cleaner code across your application.
Error Handling
Handle API errors, rate limits, and network issues gracefully.
Common Error Codes
| 401 | Invalid or missing API key |
| 429 | Rate limit exceeded (100 req/min) |
| 400 | Invalid request body |
| 500 | Server error (retry with backoff) |
Express Middleware
Protect your API endpoints with a reusable middleware.
Complete Express Example
A full example showing Tork integration in an Express chat API.
TypeScript Support
Type definitions for better IDE support and type safety.
Batch Processing
Evaluate multiple items while respecting rate limits.
Best Practices
Evaluate both input and output
Check user messages before sending to AI, and AI responses before showing to users.
Use appropriate context
Set context parameter to 'chatbot', 'content-generation', or 'api' for better detection accuracy.
Implement retry logic
Use exponential backoff for 429 and 5xx errors. Don't retry 401 or 400 errors.
Log evaluation results
Store requestId for debugging and compliance. Track blocked content patterns.
Fail gracefully
Decide whether to fail open (allow on error) or fail closed (block on error) based on your risk tolerance.
Next Steps
Explore more endpoints for jailbreak detection, RAG validation, and multi-agent orchestration.