POST /api/create-checkout-session
Creates a Stripe checkout session for purchasing credits. Returns a URL to redirect the user to Stripe’s hosted checkout page.Authentication
This endpoint requires authentication. Include valid Clerk authentication credentials in your request.Request body
The number of credits to purchase. Must be a positive integer (minimum 1).
Response
The Stripe checkout session URL to redirect the user to
Pricing
- Price per credit: $1.25 USD
- Payment methods: Credit/debit cards via Stripe
- Total amount is calculated as:
credits × $1.25
Code examples
Response example
Checkout session configuration
The Stripe checkout session includes:- Product name:
{credits} Credits - Product description:
Purchase {credits} credits for GitRead - Currency: USD
- Success URL: Returns to
/success?session_id={CHECKOUT_SESSION_ID} - Cancel URL: Returns to homepage
/
Metadata
The following metadata is attached to the Stripe session:The Clerk user ID of the purchaser
The number of credits being purchased (as a string)
Error responses
Invalid credits valueThis error occurs when:
- Credits value is not provided
- Credits is not a number
- Credits is less than 1
Authentication required
Error creating Stripe checkout session
POST /api/verify-payment
Verifies a completed Stripe checkout session and adds the purchased credits to the user’s account. This endpoint should be called after the user returns from a successful Stripe checkout.Authentication
This endpoint requires authentication. Include valid Clerk authentication credentials in your request.Request body
The Stripe checkout session ID to verify (obtained from the success URL)
Response
Indicates whether the payment was verified and credits were added
Code examples
Response example
Verification process
The endpoint performs the following checks:- Authentication: Verifies the user is authenticated
- Session validation: Retrieves the Stripe session and validates it exists
- User matching: Ensures the session’s
userIdmetadata matches the authenticated user - Payment status: Confirms the payment status is
paid - Duplicate prevention: Checks if the session has already been processed
- Credit addition: Adds credits to user’s account
- Event tracking: Marks the session as processed to prevent double-crediting
Idempotency
This endpoint is idempotent. If the same session ID is verified multiple times:- The first call adds credits and returns
{"success": true} - Subsequent calls detect the session was already processed and return
{"success": true}without adding credits again
Error responses
Various validation errorsMissing session ID:Stripe session not found:User ID mismatch:Invalid credits amount:Payment not completed:
Authentication required
Error during payment verification or credit additionor with details:
Database operations
The endpoint performs the following database operations:- Check processed events: Queries
processed_stripe_eventstable to prevent duplicate processing - Get current credits: Fetches user’s current credit balance
- Update credits: Uses upsert to add credits to the user’s account
- Record event: Inserts a record into
processed_stripe_eventswith:event_id: Formatsession_{sessionId}user_id: Clerk user IDcredits: Number of credits purchasedprocessed_at: Current timestamp
Integration flow
- User clicks “Purchase Credits”
- Client calls
/api/create-checkout-session - Client redirects user to Stripe checkout URL
- User completes payment on Stripe
- Stripe redirects to
/success?session_id=... - Client calls
/api/verify-paymentwith session ID - Credits are added to user’s account
- User can now generate READMEs