API endpoints
Overview of the RESTful API endpoints and how to use them in your application.
This documentation provides a comprehensive overview of the RESTful API endpoints available in the application and how to use them with our type-safe API client.
Available Endpoints
Items API
GET, POST, DELETE -
/api/items
- Manage items for workspacesGET, PUT, DELETE -
/api/items/:id
- Manage individual items
User API
GET -
/api/users
- Retrieve user informationGET, PUT, DELETE -
/api/users/:id
- Manage individual users
Workspace API
GET, POST -
/api/workspaces
- Manage workspacesGET, PUT, DELETE -
/api/workspaces/:id
- Manage individual workspaces
Other Endpoints
POST -
/api/feedback
- Submit user feedback
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
.
API Client Implementation
Type Definitions
Using the API Client
Basic Usage
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
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.
400 - Validation Error
Input data failed validation
401 - Unauthorized
User is not authenticated or lacks permission
404 - Not Found
Requested resource does not exist
429 - Rate Limit Exceeded
Too many requests in a short period
500 - Internal Server Error
An unexpected error occurred on the server
Permissions System
API endpoints check permissions using the hasPermission
utility
function. For more information on permissions, see the
Role-Based Access Control
documentation.
Permission Check Example
Best Practices
Request Handling
- Always validate input data with Zod
- Transform dates properly from strings
- Log important events for debugging
- Use rate limiting for all endpoints
Response Formatting
- Return consistent JSON structures
- Use appropriate HTTP status codes
- Include detailed error messages
- Use specific success messages
Security
- Always check authentication for protected routes
- Validate permissions for each operation
- Sanitize user inputs and validate against schemas
- Implement appropriate rate limiting
Data Access
- Use parameterized queries to prevent SQL injection
- Always check if resources exist before operations
- Properly scope data access to authorized workspace
- Use transactions for related operations
Client Integration
- Use the ApiClient for type-safe requests
- Handle errors consistently
- Combine with React Query for data fetching
- Provide proper loading and error states
Additional Resources
Role-Based Access Control
See the Role-Based Access Control documentation for more information on permissions.
Rate Limiting
Learn about Rate Limiting implementation in the application.