Credyt Integrate
Help the user wire Credyt into their application code. This skill works with the user's actual codebase — reading their existing code and adding Credyt integration in the right places.
The full integration guide with code examples is at
docs.credyt.ai/ai-integration.md. Fetch it for detailed patterns and examples when needed.
Understand the codebase first
Before writing any code, understand what the user has:
"Let me look at your project to understand your stack and where billing should plug in."
Check for:
- Language and framework (Node/Express, Next.js, Python/FastAPI, etc.)
- Authentication setup (how users sign up and log in)
- Where the billable activities happen in their code
- Existing environment variable patterns
Integration areas
Walk through each area. Not all will apply to every user — ask which ones they need.
1. API key setup
The Credyt API key must be stored securely on the server side — never in code that runs in the browser.
Add
to their environment variables alongside their other secrets. Show them how to create an API client or helper that attaches the key as an
header.
2. Customer creation (registration flow)
When a new user signs up in their app, create a matching Credyt customer. Find their registration/signup handler and add customer creation after successful account creation.
Key points:
- Use to link the Credyt customer to their app's user ID
- Subscribe the customer to the relevant products during creation
- Store the Credyt customer ID in their database alongside the user record
- Handle the case where the customer already exists (409/422 — look up by external_id instead)
Recurring fixed fees — pending subscriptions
If the product uses a recurring fixed fee (e.g. $20/month), the customer must pay upfront before their subscription activates. In this case the API returns a
status rather than activating immediately.
When the response status is
:
- Check the array for an action with and extract its
- Redirect the customer to that URL so they can enter their card details — include a (where to send them on success) and a (where to send them if payment fails)
- Do not activate the user's account yet — store it as pending in your database until payment is confirmed
- If the redirect link expires before the customer completes payment, fetch the customer by their Credyt ID to get a refreshed link
Once the customer pays, Credyt fires a
webhook. Listen for this event on your backend and use it to activate the user's account.
3. Usage event tracking
Find where the billable activities happen in their code and add event submission after each one. Each event needs:
- A unique ID (UUID) so the same event can't be billed twice
- The correct matching the product configuration
- A timestamp of when it happened
- Any data fields needed for pricing (volume fields, dimensions)
For
volume-based products, the event data must include the volume field (e.g.,
).
For
dimensional products, include the dimension values (e.g.,
).
4. Cost tracking (if they set up vendors)
If the user set up vendors in
, add cost data to usage events. Each event can include a
array with the vendor ID, the amount it cost, and the currency.
This is typically added right after the billable action completes, when the cost is known (e.g., after receiving the response from an AI API that includes token counts).
"Even if you're not charging users yet, attaching costs to every event lets Credyt calculate your unit economics so you can make pricing decisions based on real data."
5. Balance checks (pre-action gating)
Before expensive operations, check the customer's wallet balance. If insufficient, block the action and prompt the user to top up.
Find where billable actions are initiated (API routes, button handlers, etc.) and add a balance check before the action runs. Return a clear message if the balance is too low.
Show them how to estimate the cost of the upcoming action and compare it against the available balance.
6. Billing portal / top-up UI
Help users add funds through Credyt's billing portal. This is the simplest way to handle payments — Credyt hosts the page, handles Stripe, and redirects back to their app.
Add a "Billing" or "Add funds" link/button in their app's settings or account page. When clicked, the backend creates a billing portal session and redirects the user to the URL.
Key points:
- Portal sessions expire after 10 minutes
- Set a for where to send users after they're done
- Set a for payment failures
7. Balance display
Show the user's current balance in the app UI. Fetch from the wallet endpoint and display the available amount.
Consider where this fits in their app — sidebar, header, account page — and add it there.
Implementation approach
Don't dump all the code at once. Work through each area one at a time:
- Start with API key setup and customer creation — these are foundational
- Then add usage event tracking — this is the core billing integration
- Add balance checks to gate expensive operations
- Add billing portal and balance display for the user-facing pieces
- Add cost tracking last if applicable
After each piece, suggest they test it:
"Try creating a new account in your app and check the Credyt dashboard — you should see a new customer appear. Then we'll move on to usage tracking."
Reference
For detailed code examples, error handling patterns, and advanced topics (hybrid billing, refunds, auto top-up), point the user to: