credits-handler
Manage the credit system (allocation, purchasing, usage). Use when adding credit types, configuring pricing, or building credit UI.
Best use case
credits-handler is best used when you need a repeatable AI agent workflow instead of a one-off prompt.
Manage the credit system (allocation, purchasing, usage). Use when adding credit types, configuring pricing, or building credit UI.
Teams using credits-handler should expect a more consistent output, faster repeated execution, less prompt rewriting.
When to use this skill
- You want a reusable workflow that can be run more than once with consistent structure.
When not to use this skill
- You only need a quick one-off answer and do not need a reusable workflow.
- You cannot install or maintain the underlying files, dependencies, or repository context.
Installation
Claude Code / Cursor / Codex
Manual Installation
- Download SKILL.md from GitHub
- Place it in
.claude/skills/credits-handler/SKILL.mdinside your project - Restart your AI agent — it will auto-discover the skill
How credits-handler Compares
| Feature / Agent | credits-handler | Standard Approach |
|---|---|---|
| Platform Support | Not specified | Limited / Varies |
| Context Awareness | High | Baseline |
| Installation Complexity | Unknown | N/A |
Frequently Asked Questions
What does this skill do?
Manage the credit system (allocation, purchasing, usage). Use when adding credit types, configuring pricing, or building credit UI.
Where can I find the source code?
You can find the source code on GitHub using the link provided at the top of the page.
SKILL.md Source
# Credits Handler
This skill guides you through the entire credit system, from backend configuration to frontend UI implementation.
## 1. Configuration
All credit configuration lives in `src/lib/credits/config.ts`.
### Adding a New Credit Type
1. **Define the Type**: Add the new type to `creditTypeSchema` enum.
```typescript
export const creditTypeSchema = z.enum([
"image_generation",
"video_generation",
"your_new_credit_type" // Add this
]);
```
2. **Configure Pricing & Metadata**: Add an entry to `creditsConfig`.
```typescript
your_new_credit_type: {
name: "New Credit Name",
currency: "USD",
minimumAmount: 10,
// Option A: Fixed Slabs
slabs: [
{ from: 1, to: 100, pricePerUnit: 0.10 },
{ from: 101, to: 1000, pricePerUnit: 0.08 },
],
// Option B: Dynamic Calculator (e.g., based on user plan)
priceCalculator: (amount, userPlan) => {
// Logic here
return amount * 0.1;
}
}
```
3. **Plan Allocations (Optional)**: Define how many credits are given when subscribing to a plan in `onPlanChangeCredits`.
## 2. UI Implementation: Buying Credits
To let users purchase credits, use the `useBuyCredits` hook. This hook handles price calculation (factoring in plan discounts) and generating checkout URLs.
### Key Hook: `useBuyCredits`
**Location**: `src/lib/credits/useBuyCredits.ts`
#### Usage
```typescript
import useBuyCredits from "@/lib/credits/useBuyCredits";
import { PlanProvider } from "@/lib/plans/getSubscribeUrl";
const {
price, // Calculated total price (number | undefined)
isLoading, // Price calculation in progress
error, // Error state
getBuyCreditsUrl // Function to generate payment URL
} = useBuyCredits(creditType, amount);
```
### Example: Building a Pricing Card
Here is a pattern for creating a credit purchase UI, similar to `src/components/website/website-credits-section.tsx`.
```tsx
"use client";
import { useState } from "react";
import useBuyCredits from "@/lib/credits/useBuyCredits";
import { PlanProvider } from "@/lib/plans/getSubscribeUrl";
import { Button } from "@/components/ui/button";
import { Loader2 } from "lucide-react";
// 1. Define your packages
const PACKAGE = {
credits: 100,
name: "Starter Pack",
};
export function BuyCreditsCard({ creditType }: { creditType: "image_generation" }) {
const [provider] = useState(PlanProvider.STRIPE); // or LEMONSQUEEZY
// 2. Call the hook
const { price, isLoading, error, getBuyCreditsUrl } = useBuyCredits(
creditType,
PACKAGE.credits
);
// 3. Handle Purchase
const handleBuy = () => {
const url = getBuyCreditsUrl(provider);
window.location.href = url;
};
// 4. Render UI
return (
<div className="border p-4 rounded-lg">
<h3>{PACKAGE.name}</h3>
<div className="text-2xl font-bold">
{isLoading ? (
<Loader2 className="animate-spin" />
) : (
`$${price?.toFixed(2) || "0.00"}`
)}
</div>
<Button
onClick={handleBuy}
disabled={isLoading || !price}
className="w-full mt-4"
>
Buy {PACKAGE.credits} Credits
</Button>
{error && <p className="text-red-500 text-sm">{error.message}</p>}
</div>
);
}
```
## 3. UI Implementation: Displaying Credits
To show the user's current balance, use the `useCredits` hook.
### Key Hook: `useCredits`
**Location**: `src/lib/users/useCredits.ts`
#### Return Data Structure
```typescript
const {
credits, // Record<string, number> | undefined
// e.g. { "image_generation": 100, "video_generation": 50 }
isLoading, // boolean
error, // any
mutate // SWR mutate function to refresh data
} = useCredits();
```
#### Usage Example
```tsx
import useCredits from "@/lib/users/useCredits";
export function CreditBalance() {
const { credits, isLoading } = useCredits();
if (isLoading) return <div>Loading...</div>;
return (
<div>
Image Credits: {credits?.image_generation || 0}
</div>
);
}
```
## 4. Core Operations (Backend)
These functions are used in API routes and webhooks.
### Allocating Credits (e.g., on Plan Change)
Use `allocatePlanCredits` to give users credits when they subscribe/upgrade.
- **File**: `src/lib/credits/allocatePlanCredits.ts`
- **Input**: `userId`, `planId`, `paymentId` (for idempotency).
- **Behavior**: Checks `onPlanChangeCredits` config and adds credits if applicable.
### Adding/Deducting Credits
To manually manipulate balances, use helpers from `src/lib/credits/recalculate.ts` (e.g., `addCredits`, `deductCredits`).
*Note: Always ensure you have a unique `paymentId` or transaction reference when adding credits to prevent duplicates.*
## 5. Checking Balance (Backend/Common)
Use `canDeductCredits` before performing an action.
```typescript
import { canDeductCredits } from "@/lib/credits/credits";
// Check if user has enough credits
const hasBalance = canDeductCredits(
"image_generation",
1,
user // Must contain { credits: { ... } }
);
if (!hasBalance) {
throw new Error("Insufficient credits");
}
```
## 6. Reference
For deep dives into database schema and architecture, see [reference.md](reference.md).Related Skills
websocket-handler-setup
Websocket Handler Setup - Auto-activating skill for Backend Development. Triggers on: websocket handler setup, websocket handler setup Part of the Backend Development skill category.
webhook-retry-handler
Webhook Retry Handler - Auto-activating skill for API Integration. Triggers on: webhook retry handler, webhook retry handler Part of the API Integration skill category.
timeout-handler
Timeout Handler - Auto-activating skill for API Integration. Triggers on: timeout handler, timeout handler Part of the API Integration skill category.
sorting-parameter-handler
Sorting Parameter Handler - Auto-activating skill for API Development. Triggers on: sorting parameter handler, sorting parameter handler Part of the API Development skill category.
oauth-callback-handler
Oauth Callback Handler - Auto-activating skill for API Integration. Triggers on: oauth callback handler, oauth callback handler Part of the API Integration skill category.
long-polling-handler
Long Polling Handler - Auto-activating skill for API Integration. Triggers on: long polling handler, long polling handler Part of the API Integration skill category.
kubernetes-configmap-handler
Kubernetes Configmap Handler - Auto-activating skill for DevOps Advanced. Triggers on: kubernetes configmap handler, kubernetes configmap handler Part of the DevOps Advanced skill category.
go-handler-generator
Go Handler Generator - Auto-activating skill for Backend Development. Triggers on: go handler generator, go handler generator Part of the Backend Development skill category.
etag-handler
Etag Handler - Auto-activating skill for API Development. Triggers on: etag handler, etag handler Part of the API Development skill category.
error-handler-middleware
Error Handler Middleware - Auto-activating skill for Backend Development. Triggers on: error handler middleware, error handler middleware Part of the Backend Development skill category.
environment-variables-handler
Environment Variables Handler - Auto-activating skill for DevOps Basics. Triggers on: environment variables handler, environment variables handler Part of the DevOps Basics skill category.
creating-webhook-handlers
Create webhook endpoints with signature verification, retry logic, and payload validation. Use when receiving and processing webhook events. Trigger with phrases like "create webhook", "handle webhook events", or "setup webhook handler".