Skip to content

Conversation

devroy10
Copy link

@devroy10 devroy10 commented Sep 17, 2025

fix #7

Overview

This PR adds a complete Astro integration example with-astro that demonstrates how to build a products-services application using Polar's SDK and Astro adapter for seamless webhooks handling, customer portal integration, and checkout session creation.

Implementation Checklist

Following the guidelines from https://docs.polar.sh/integrate/sdk/adapters/astro:

  • ensure best practices with the use of env and other underlying services by the framework
  • name it as with-astro
  • use prettier as done in https://github.com/polarsource/examples/tree/main/export-all-customers-to-csv
  • install the latest version of Polar and Astro
  • maintain seperate API routes for each, Webhooks handler, Customer Portal handler and Checkout handler
  • add an optional build-time script that validates if the access token used is valid for creating a checkout session in the relevant Polar mode (sandbox/production)
  • create an installation guide like as in https://github.com/neondatabase/examples/tree/main/with-nestjs
  • create the .env.example with the comments to the guides links of where the values can be obtained from
  • share the deployed application to Vercel with the recording of all the 3 functionalities working

Project Structure

with-astro/
├── src/
│   ├── components/
│   │   ├── ProductCard.astro
│   │   └── ProductsGrid.astro
│   ├── layouts/
│   │   └── Layout.astro
│   ├── lib/
│   │   ├── config.ts
│   │   ├── getProducts.ts
│   │   └── polarClient.ts
│   ├── pages/
│   │   ├── api/
│   │   │   ├── webhooks/
│   │   │   │   └── polar.ts        # Webhook handler
│   │   │   ├── checkout.ts         # Checkout creation
│   │   │   └── customer-portal.ts  # Customer portal
│   │   ├── index.astro
│   │   ├── success.astro
│   │   └── customer-portal.astro
│   └── styles/
│       └── global.css
├── scripts/
│   └── validateAccessToken.ts      # Build-time validation
├── astro.config.mjs                # Astro config file
├── .env.example                    # Environment template
├── README.md                       # Installation guide
└── package.json

Key Features

Environment Configuration

  • POLAR_MODE environment variable for seamless sandbox/production switching
  • Comprehensive token validation with masked logging
  • Type-safe environment variable handling
  • Fallback defaults for development
// Automatic environment detection
const accessToken = POLAR_MODE === 'production' ? POLAR_ACCESS_TOKEN : SANDBOX_POLAR_ACCESS_TOKEN
const server = POLAR_MODE === 'production' ? 'production' : 'sandbox'

Checkout Integration

Customer Portal

  • Email-based customer lookup with auto-creation
  • Secure customer ID resolution

Webhooks Handler

  • Event validation and processing
  • Type-safe webhook payload handling

Validation Script

<! --Optional build-time script that validates: -->

  • API connectivity
  • Token permissions
  • Checkout session creation
  • Products, customers, orders access
  • Environment configuration

Documentation

Installation Guide

Comprehensive README.md following the recommended stylistic guide pattern from

Environment Configuration

.env.example with detailed comments linking to Polar documentation

Deployment and Demo

Vercel Deployment

Live Deployment: https://polar-astro.vercel.app

  • Optimized for Vercel deployment
  • Environment variables properly configured

Demo Recording

Video Recording

video-lite.mp4

Recording demonstrates:

  1. Product Listing: Homepage with products from Polar
  2. Checkout Flow: Complete purchase process
  3. Customer Portal: Account management and subscription details
  4. Webhook Processing: Real-time event handling

Related Documentation

This example provides a solid foundation for building products-services applications with Polar and Astro, following all best practices and providing comprehensive documentation for developers to get started quickly.

Fixes #7

Copy link

vercel bot commented Sep 17, 2025

@devroy10 is attempting to deploy a commit to the polar-sh Team on Vercel.

A member of the Team first needs to authorize it.

@devroy10
Copy link
Author

@rishi-raj-jain ready for a review

@rishi-raj-jain rishi-raj-jain self-requested a review September 17, 2025 08:11
@devroy10
Copy link
Author

devroy10 commented Sep 17, 2025

@rishi-raj-jain requested changes have been applied, review when you can

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Astro example with Webhooks, Customer Portal and Checkout creation
2 participants