A serverless calculator API powered by SpinAI that performs mathematical operations using a natural language interface.
api/- Contains serverless function endpointsactions/- Contains calculator operations (sum, minus, multiply, divide)dev-server.ts- Local development server that mimics Vercel's serverless environment
Our custom development server mimics Vercel's serverless environment but with better debugging capabilities and no timeout issues:
npm run devThis starts a server at http://localhost:3000 that:
- Automatically discovers all API endpoints in the
api/directory - Maps
/api/endpointand/endpointto the corresponding handlers - Provides detailed error messages for debugging
You can also use Vercel's official development environment:
npm run vercel-dev
# or
vercel devSend a POST request to your API endpoint:
curl -X POST http://localhost:3000/api -H "Content-Type: application/json" -d '{"input": "5+7"}'Example request body:
{
"input": "5+7"
}Example response:
{
"response": {
"finalNumber": 12
},
"messages": [...]
}Before deploying, ensure your vercel.json file is properly configured:
{
"version": 2,
"functions": {
"api/**/*.ts": {
"maxDuration": 60
}
}
}IMPORTANT: The
maxDurationsetting is crucial for preventing logging timeouts. The default 10-second timeout is often not enough for SpinAI agents to complete execution and send logs properly.
Set these environment variables in your Vercel project:
SPINAI_API_KEY- Your SpinAI API keyLOGGING_PUBLIC_URL- (Optional) Public endpoint for logs
vercel deploy
# or for production
vercel deploy --prodYour API will be accessible at:
https://your-project-name.vercel.app/api
If you're experiencing empty log bodies in your logging endpoint:
- Increase function timeout: Ensure
maxDurationis set to at least 60 seconds invercel.json - Check connection closure: Vercel may close connections before logs are fully transmitted (see issue: denoland/deno#27132)
- Error handling: The code includes special handling for
BadResourceerrors that occur when connections close prematurely
If your function works locally but fails in production:
- Check environment variables in Vercel dashboard
- Ensure
maxDurationis properly set invercel.json - Look for timeout errors in Vercel function logs