creda-sdk
v1.0.0
Published
Simple SDK for managing loyalty points using Pocketbase.
Readme
Creda Loyalty SDK
Simple SDK for managing loyalty points using Pocketbase.
Installation
npm install creda-sdkQuick Start
Step 1: Import and Initialize
const { Loyalty } = require('creda-sdk');
const loyalty = new Loyalty('your-secret-key');Step 2: Give Points to Users
// Give 100 points to a user
const newBalance = await loyalty.mint('[email protected]', 100);
console.log(`New balance: ${newBalance}`);Step 3: Check User Balance
// Check how many points a user has
const balance = await loyalty.getBalance('[email protected]');
console.log(`User has ${balance} points`);Step 4: Transfer Points Between Users
// Send 50 points from one user to another
await loyalty.distribute('[email protected]', '[email protected]', 50);
console.log('Transfer completed');Step 5: Deduct Points
// Remove 25 points from a user (when they make a purchase)
const remainingBalance = await loyalty.burn('[email protected]', 25);
console.log(`Remaining balance: ${remainingBalance}`);Complete Example
const { Loyalty } = require('creda-sdk');
async function example() {
const loyalty = new Loyalty('your-secret-key');
try {
// Give points
await loyalty.mint('[email protected]', 100);
// Check balance
const balance = await loyalty.getBalance('[email protected]');
console.log(`Alice has ${balance} points`);
// Transfer points
await loyalty.distribute('[email protected]', '[email protected]', 30);
// Use points
await loyalty.burn('[email protected]', 20);
console.log('All operations completed!');
} catch (error) {
console.error('Error:', error.message);
}
}
example();API Methods
| Method | Description | Returns |
|--------|-------------|---------|
| mint(email, amount) | Add points to user | New balance |
| distribute(fromEmail, toEmail, amount) | Transfer points | true |
| getBalance(email) | Get user's points | Current balance |
| burn(email, amount) | Remove points | New balance |
Error Handling
The SDK will throw errors for:
- Invalid secret key
- Insufficient balance for transfers/burns
- Network connection issues
Always wrap SDK calls in try/catch blocks. z
Publishing to NPM
Step 1: Prepare for Publishing
# Login to NPM (you need an NPM account)
npm login
# Update package.json version if needed
npm version patch # or minor/majorStep 2: Publish
npm publishStep 3: Install from NPM
Once published, users can install your package:
npm install creda-sdkUsing with Next.js
Your SDK works perfectly with Next.js! Here are examples:
API Route (pages/api/loyalty.js or app/api/loyalty/route.js)
import { Loyalty } from 'creda-sdk';
const loyalty = new Loyalty(process.env.LOYALTY_SECRET_KEY);
// For Pages Router
export default async function handler(req, res) {
try {
const { email, amount } = req.body;
const balance = await loyalty.mint(email, amount);
res.json({ success: true, balance });
} catch (error) {
res.status(500).json({ error: error.message });
}
}
// For App Router
export async function POST(request) {
try {
const { email, amount } = await request.json();
const balance = await loyalty.mint(email, amount);
return Response.json({ success: true, balance });
} catch (error) {
return Response.json({ error: error.message }, { status: 500 });
}
}Client-side Usage (React Component)
'use client'; // for App Router
import { useState } from 'react';
export default function LoyaltyComponent() {
const [balance, setBalance] = useState(0);
const [email, setEmail] = useState('');
const handleMint = async () => {
try {
const response = await fetch('/api/loyalty', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email, amount: 100 })
});
const data = await response.json();
setBalance(data.balance);
} catch (error) {
console.error('Error:', error);
}
};
return (
<div>
<input
value={email}
onChange={(e) => setEmail(e.target.value)}
placeholder="Enter email"
/>
<button onClick={handleMint}>Give 100 Points</button>
<p>Balance: {balance}</p>
</div>
);
}Environment Variables
Add to your .env.local file:
LOYALTY_SECRET_KEY=your-secret-key-hereReady to Use!
Your SDK is compatible with:
- ✅ Node.js applications
- ✅ Next.js (both Pages and App Router)
- ✅ Express.js servers
- ✅ Any JavaScript/TypeScript project
