Installation
Setup
Configure environment variables
Add your Polar access token to your environment variables:
.env.local
Use
NEXT_PUBLIC_ prefix only for client-side variables. Keep your access token server-side only.Create a Polar client instance
For App Router, create a utility function to initialize the Polar client:
utils/polar.ts
App Router Patterns
Server Actions
Use Server Actions for mutations:app/actions.ts
app/components/BuyButton.tsx
API Routes
For API endpoints, use Route Handlers:app/api/webhooks/polar/route.ts
Metadata and SEO
Generate dynamic metadata using Polar data:app/products/[id]/page.tsx
Pages Router Patterns
Server-Side Rendering
pages/products/index.tsx
API Routes (Pages Router)
pages/api/webhooks/polar.ts
Error Handling
Handle Polar SDK errors gracefully:utils/error-handler.ts
TypeScript Support
The Polar SDK is fully typed. Import types directly:Best Practices
Keep tokens secure
Never expose your access token in client-side code. Always use server-side functions or API routes.
Use error boundaries
Wrap your components in error boundaries to handle API failures gracefully.
Cache strategically
Use Next.js caching features with
revalidatePath and revalidateTag for optimal performance.Type safety
Leverage TypeScript types from the SDK for better developer experience.
Next Steps
Checkout Integration
Learn how to implement Polar Checkout in your Next.js app.
Webhooks
Set up webhook handlers to receive real-time events.
Customer Portal
Embed the customer portal in your application.
API Reference
Explore the complete Polar API.