API Reference
Overview of the RESTful API endpoints and how to use them
Available Endpoints
/api/items
Manage items for workspaces
/api/items/:id
Manage individual items
/api/feedback
Submit user feedback
/api/users
Retrieve user information
/api/users/:id
Manage individual users
/api/workspaces
Manage workspaces
/api/workspaces/:id
Manage individual workspaces
Type-Safe API Client
The application provides a type-safe API client to simplify API requests and ensure type safety across the codebase. The client is implemented in lib/api-client.ts
.
Type-Safe API Client
Type Definitions
Using the API Client
Example of using the API client to fetch items:
React Query Integration
Example of using the API client with React Query:
Authentication & Authorization
All API endpoints require authentication through Auth.js sessions. The server retrieves the current user using the getCurrentUser()
function.
Authentication Flow
// 1. Retrieve the current user
const { user } = await getCurrentUser()
// 2. Check if the user exists
if (!user) {
return NextResponse.json(
{ error: "Unauthorized" },
{ status: 401 }
)
}
// 3. Proceed with authenticated operations
// ...
Rate Limiting
All API endpoints implement rate limiting using Upstash Ratelimit to prevent abuse. Rate limits are defined in the constants.ts
file.
Rate Limit Implementation
Working with Items
Retrieving Items
Get all items or filter by workspace slug:
Creating Items
Create a new item in a workspace:
Deleting Items
Delete all items in a workspace (requires DELETE_WORKSPACE permission):
Error Handling
The API consistently handles errors and returns appropriate status codes and error messages.
Validation Error
Input data failed validation
{
error: "Validation Error",
details: [
{
code: "invalid_type",
path: ["values", "name"],
message: "Required"
}
]
}
Unauthorized
User is not authenticated or lacks permission
{
error: "Unauthorized"
}
Not Found
Requested resource does not exist
{
error: "Workspace not found"
}
Rate Limit Exceeded
Too many requests in a short period
{
error: "Rate limit exceeded"
}
Internal Server Error
An unexpected error occurred on the server
{
error: "Internal Server Error",
details: "Error message"
}
Permissions System
API endpoints check permissions using the hasPermission
utility function.
Permission Check Example
Best Practices
Request Handling
- 1Always validate input data with Zod
- 2Transform dates properly from strings
- 3Log important events for debugging
- 4Use rate limiting for all endpoints
Response Formatting
- 1Return consistent JSON structures
- 2Use appropriate HTTP status codes
- 3Include detailed error messages
- 4Use specific success messages
Security
- 1Always check authentication for protected routes
- 2Validate permissions for each operation
- 3Sanitize user inputs and validate against schemas
- 4Implement appropriate rate limiting
Data Access
- 1Use parameterized queries to prevent SQL injection
- 2Always check if resources exist before operations
- 3Properly scope data access to authorized workspace
- 4Use transactions for related operations
Client Integration
- 1Use the ApiClient for type-safe requests
- 2Handle errors consistently
- 3Combine with React Query for data fetching
- 4Provide proper loading and error states