Skip to content

chrisbuildsonline/easy-polar-nextjs

Repository files navigation

🚀 Simple Polar.sh Subscription

Screenshot_2025-08-18 at 20 47 13

A super lightweight Next.js demo showing how to integrate Polar.sh subscriptions with just a few simple functions.

✨ Features

  • 5 Simple Functions - All you need to manage subscriptions
  • Real-time Data - Always fetches fresh data from Polar.sh
  • Automatic Upgrades - Seamlessly switch between plans
  • Price Display - Shows formatted pricing with currency
  • Success Redirects - Handles payment success gracefully
  • Ultra Lightweight - Minimal dependencies and files

📁 Project Structure

├── app/
│   ├── api/polar/route.ts          # Single API endpoint for all Polar operations
│   ├── layout.tsx                   # Basic app layout
│   └── page.tsx                     # Main page
├── components/
│   └── SimplePolar.tsx              # Main subscription dashboard component
├── lib/
│   ├── polar.ts                     # 5 simple functions for everything
│   └── polar-client.ts              # Polar.sh API client (internal)
└── .env.local                       # Configuration

🚀 Quick Start

1. Clone & Install

git clone <your-repo>
cd nextjs
npm install

2. Configure Environment

Create .env.local:

# Your Polar.sh access token (get from dashboard)
POLAR_ACCESS_TOKEN=polar_oat_your_token_here

# Where users return after successful payment
POLAR_SUCCESS_URL=http://localhost:3000?success=true

# Email to test subscriptions with (any email works)
DEMO_USER_EMAIL=your-email@example.com

3. Get Your Polar.sh Token

  1. Go to Polar.sh Dashboard
  2. Navigate to Settings → API Keys
  3. Create a new Personal Access Token
  4. Copy it to POLAR_ACCESS_TOKEN in your .env.local

4. Run the Demo

npm run dev

Open http://localhost:3000 in your browser.

📚 How to Use

The demo shows a subscription dashboard that:

  1. Shows Current Plan - Displays any active subscription for the configured email
  2. Lists Available Plans - Shows all your Polar.sh products with prices
  3. Handle Upgrades - Automatically cancels old subscription and creates new one
  4. Success Messages - Shows confirmation when payments succeed

Changing the Test Email

Simply update DEMO_USER_EMAIL in .env.local and restart the server. The dashboard will show subscription data for that email address.

🔧 The 5 Simple Functions

All subscription management is handled by these functions in lib/polar.ts:

// Get user's current subscription
const subscription = await getUserSubscription()

// Get all available products/plans
const products = await getProducts()

// Start checkout process for a product
await startCheckout(productId)

// Cancel an active subscription
await cancelSubscription(subscriptionId)

// Check if payment was successful from URL
const success = checkPaymentSuccess()

📝 API Endpoints

The single API endpoint handles everything:

  • GET /api/polar?action=products - Get all products
  • GET /api/polar?action=subscription - Get user's subscription
  • POST /api/polar?action=checkout - Create checkout session
  • POST /api/polar?action=cancel - Cancel subscription

💡 Integration Tips

Adding to Your Existing App

  1. Copy these files to your project:

    • lib/polar.ts - The 5 simple functions
    • lib/polar-client.ts - Internal API client
    • app/api/polar/route.ts - API endpoint
  2. Install dependencies:

    npm install next react
  3. Set up environment variables (see above)

  4. Use the functions in your components:

    import { getProducts, startCheckout } from './lib/polar'
    
    // In your component
    const products = await getProducts()
    // Show products, then on click:
    await startCheckout(productId)

Customizing the UI

The SimplePolar.tsx component is just an example. You can:

  • Replace it with your own design
  • Use the 5 functions in lib/polar.ts anywhere in your app
  • Style it to match your brand
  • Add additional features like trial periods, etc.

Production Deployment

  1. Update POLAR_SUCCESS_URL to your production domain
  2. Make sure your Polar.sh products are published
  3. Test with real email addresses
  4. Deploy normally (Vercel, Netlify, etc.)

💾 Database Integration Examples

The code includes helpful comments showing where to add database logic. Here are complete examples:

🐛 Troubleshooting

"Checkout failed"

  • Check your POLAR_ACCESS_TOKEN is correct
  • Verify the product exists and is published in Polar.sh

"No subscription found"

  • The email in DEMO_USER_EMAIL doesn't have an active subscription
  • Try subscribing to a plan first

Redirect not working

  • Check POLAR_SUCCESS_URL matches your domain
  • Make sure the URL includes ?success=true

🤝 Support

📄 License

MIT - Use this however you want!


Built with ❤️ for the Polar.sh community

About

Easy Polar.sh integration for NextJS

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors