Overview
The Subscriptions API handles billing and subscription management through Stripe webhooks. This is primarily used by Khoj Cloud to process subscription events automatically.Webhook Endpoint
Stripe sends events to this endpoint to update subscription status.- New subscription payments
- Subscription updates (upgrades, downgrades)
- Subscription cancellations
- Subscription deletions
Supported Events
Stripe event type. Supported values:
invoice.paid- Payment successful, activate/renew subscriptioncustomer.subscription.updated- Subscription modified (cancelled, resubscribed, etc.)customer.subscription.deleted- Subscription permanently deleted
Webhook Security
The endpoint verifies webhook signatures using Stripe’s signing secret to ensure authenticity.Event Processing
invoice.paid
When an invoice is paid successfully:- Retrieves the subscription and renewal date
- Marks the user as subscribed
- Updates the renewal date to the next billing period
- Creates a new user account if this is their first payment
customer.subscription.updated
When a subscription is modified:- Checks if subscription is set to cancel at period end
- Updates
is_recurringstatus accordingly - Updates renewal date if changed
- User cancels renewal (sets
cancel_at_period_endto true) - User resumes cancelled subscription (sets
cancel_at_period_endto false) - Subscription plan changes
customer.subscription.deleted
When a subscription is deleted:- Marks user as unsubscribed
- Clears renewal date
- Resets subscription to free tier
Response
{"success": false}.
User Subscription Management
These endpoints allow users to manage their own subscriptions.Update Subscription
Cancel or resume your subscription.Cancel Subscription
Resume Subscription
Operation to perform:
cancel- Cancel recurring subscription (access until end of billing period)resubscribe- Resume a cancelled subscription
When you cancel, you retain access until the end of your current billing period. The subscription won’t auto-renew.
Subscription Tiers
Khoj Cloud offers different subscription tiers with varying features:Free Tier
- Basic chat access with limited daily messages
- 50MB indexed content
- Lower rate limits
- Access to basic models
Premium Tier
- Higher message limits (75+ per day)
- 500MB indexed content
- Higher rate limits across all endpoints
- Access to advanced models (GPT-4, Claude Opus, etc.)
- Priority support
Checking Subscription Status
You can check if a user has an active subscription via the user info endpoint:Indicates whether the user has an active premium subscription
Product Verification
Khoj Cloud verifies that webhook events are for the official Khoj product:Configuration
For self-hosted Khoj instances that want to enable billing:Environment Variables
Enable Billing
In your Khoj configuration:Setting Up Stripe Webhooks
If you’re running your own Khoj instance with billing:Create Webhook Endpoint
In Stripe Dashboard, create a webhook endpoint pointing to
https://your-domain.com/api/subscriptionSelect Events
Subscribe to these events:
invoice.paidcustomer.subscription.updatedcustomer.subscription.deleted
Subscription Lifecycle
Error Handling
Invalid Signature
Invalid Product
No Subscription Found
Rate Limits
Webhook endpoints are not rate-limited as they’re called by Stripe, not end users. User-facing subscription management endpoints follow standard API rate limits.Best Practices
Webhook Monitoring
Monitor webhook processing logs to catch and resolve issues quickly.
Idempotency
Stripe may send duplicate events. Handle them idempotently.
Test Mode
Always test webhook processing in Stripe test mode first.
Signature Verification
Never skip signature verification in production.
Self-Hosted Considerations
For self-hosted Khoj:- Subscriptions are optional
- Can run in anonymous mode without any billing
- Can implement your own billing logic
- All API features work without subscriptions (no rate limits by default)
Next Steps
Authentication
Set up API authentication
User Settings
Manage user account settings
