Complete guide to integrating the SDK with Next.js applications
The SDK works seamlessly with Next.js applications, supporting both client and server-side rendering with proper token management.
For Next.js applications, you need token storage that works in both client and server environments. The built-in CookieTokenStorage
only works client-side, so youβll need a custom implementation:
Create a single SDK instance that works everywhere:
Set up your environment variables:
Only expose the API key client-side if your API endpoints support it. Keep sensitive keys server-side only when possible.
Use the SDK in server components and API routes:
Use the SDK in client components:
Use the SDK in API routes for backend operations:
Handle authentication in both server and client contexts:
Create middleware to protect routes:
Use cookies for authentication in full-stack Next.js apps to ensure tokens are available to both client and server components
Keep API keys secure and use appropriate Next.js environment variable patterns
Implement error boundaries to handle SDK errors gracefully in your React components
Leverage the SDKβs TypeScript support for better development experience and fewer runtime errors
This pattern ensures your authentication works consistently across Next.js server components, client components, and API routes. The cookie-based approach is recommended for full-stack Next.js applications.