Quick Start
This guide will get you up and running with next-api-layer in under 5 minutes.
1. Create the Proxy Configuration#
Create a proxy.ts file in your src directory:
TypeScript
Loading...
2. Create the Catch-All API Route#
Create the API route that will handle all requests:
TypeScript
Loading...
3. Make API Calls#
Now you can use the api client anywhere in your app:
TypeScript
Loading...
TypeScript
Loading...
4. Add Client-Side Auth (Optional)#
For client-side authentication state, add the AuthProvider:
TypeScript
Loading...
Then use the useAuth hook:
TypeScript
Loading...
What's Happening?#
When you make a request through the proxy:
- Token Check: The proxy checks for a valid JWT token in cookies
- Validation: If expired, it automatically refreshes the token
- Guest Fallback: If no token exists and guest is enabled, it creates a guest token
- Request Forward: The request is forwarded to your backend with the token
- Response Security: The response is sanitized for XSS protection
- Cookie Update: If token was refreshed, new cookies are set
All of this happens automatically. You just write api.get('endpoint').
Next Steps#
- How It Works - Deep dive into the architecture
- Auth Proxy Configuration - Customize the proxy
- Security Features - CSRF, rate limiting, and more