API Routes
This section documents all API endpoints available in CraftJS.
Overview
CraftJS provides RESTful API endpoints for all major features:
| Endpoint | Method | Description |
|---|---|---|
/api/auth/* | Various | Authentication (Better Auth) |
/api/chat | POST | AI chat completions |
/api/chats | GET | List user chats |
/api/chats/[id] | GET/DELETE | Get or delete a chat |
/api/upload | POST | Get file upload URL |
/api/checkout | POST | Create payment checkout |
/api/billing/portal | POST | Access billing portal |
/api/webhooks/* | POST | Webhook handlers |
Authentication
All API routes (except webhooks and public endpoints) require authentication. Include the session cookie or use the auth header.
Session Cookie
Authentication is automatic when using cookies (default browser behavior).
API Key (Coming Soon)
For programmatic access:
curl -X POST https://yourapp.com/api/chat \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"messages": [...]}'Rate Limiting
API endpoints are rate limited per user:
| Plan | Limit |
|---|---|
| Free | 10 requests/minute |
| Pro | 100 requests/minute |
| Enterprise | 1000 requests/minute |
Rate limit headers are included in responses:
X-RateLimit-Remaining: 9
X-RateLimit-Reset: 1699999999999Error Responses
All errors follow a consistent format:
{
"error": "Error message",
"code": "ERROR_CODE",
"details": {}
}Common Error Codes
| Status | Code | Description |
|---|---|---|
| 400 | INVALID_REQUEST | Invalid request body |
| 401 | UNAUTHORIZED | Not authenticated |
| 403 | FORBIDDEN | Not authorized for this resource |
| 404 | NOT_FOUND | Resource not found |
| 429 | RATE_LIMITED | Rate limit exceeded |
| 500 | INTERNAL_ERROR | Server error |
CORS
CORS is configured to allow requests from:
- Your production domain
localhostin development
To customize, update next.config.ts:
async headers() {
return [
{
source: "/api/:path*",
headers: [
{ key: "Access-Control-Allow-Origin", value: "https://yourdomain.com" },
{ key: "Access-Control-Allow-Methods", value: "GET,POST,DELETE" },
{ key: "Access-Control-Allow-Headers", value: "Content-Type, Authorization" },
],
},
]
}API Sections
Last updated on