Table of Contents
- Installation
- Basic Setup
- Protecting Endpoints
- Authentication Integration
- Error Handling
- Advanced Usage
- Complete Example
Installation
Install the required packages:Basic Setup
1. Initialize SolvaPay
Create a SolvaPay instance in your Express app:2. Protect Your First Endpoint
Wrap your business logic with thepayable.http() adapter:
- Check if the customer has a valid purchase
- Track usage and enforce limits
- Return a paywall error with checkout URL if needed
Protecting Endpoints
Multiple Endpoints
Create onepayable handler and apply it to all protected endpoints:
Accessing Request Data
The HTTP adapter passes the Express request object to your business logic:Authentication Integration
SolvaPay needs to identify customers. You can pass customer references in several ways:Option 1: Custom Header (Recommended)
Extract customer reference from a custom header:Option 2: JWT Token
Extract customer ID from a JWT token:Option 3: Custom Customer Reference Extraction
Use thegetCustomerRef option for more complex scenarios:
Error Handling
Basic Error Handling
The HTTP adapter routes paywall gate outcomes through aformatGate
channel that emits a 402 response with the standard JSON body
({success:false, error, product, checkoutUrl, message, ...}). Merchants
don’t need to catch anything for the happy path:
Custom Paywall Responses
Usepaywall.decide() directly when you want full control over the
402 response shape (e.g. a hand-rolled route that doesn’t use
payable.http()):
Legacy PaywallError Compat
Consumers that still try/catch a PaywallError keep working —
PaywallError is exported as a compat shim for merchant code that
throws from deep inside business logic:
Advanced Usage
Custom Customer Reference Extraction
Override customer reference extraction per endpoint:Response Transformation
Transform responses before sending:Response Formatting
The HTTP adapter automatically formats responses. Your business logic should return:- Object: Sent as JSON with 200 status
- Error: Thrown as exception (PaywallError handled automatically)
Complete Example
Here’s a complete Express.js application with SolvaPay integration:Testing the Example
Best Practices
- Extract Customer Reference Early: Use middleware to extract and validate customer references before they reach protected endpoints.
- Handle Paywall Errors Gracefully: Provide clear error messages and checkout URLs to users.
- Use Environment Variables: Store API keys and configuration in environment variables.
- Separate Business Logic: Keep your business logic functions separate from route handlers for better testability.
- Type Safety: Use TypeScript for better type safety and developer experience.
- Error Logging: Log errors appropriately for debugging while keeping sensitive information secure.
Next Steps
- Next.js Integration Guide - Learn how to integrate with Next.js
- Webhooks - Handle async purchase and subscription events
- Custom Authentication Adapters - Build custom auth adapters
- API Reference - Full API documentation