@codeswayam/api-client
v1.3.1
Published
CodeSwayam Professional SaaS SDK
Readme
@codeswayam/api-client
Low-level HTTP client and typed API functions for the Codeswayam platform. Used internally by @codeswayam/auth — most apps should use that package instead.
Installation
npm install @codeswayam/api-clientWhen to use this directly
Use @codeswayam/api-client directly when:
- Writing server-side code (NestJS services, Next.js server actions)
- Building admin tools that need raw API access
- You need functions not exposed by
@codeswayam/auth
For everything else, use @codeswayam/auth.
SDK Class
import { CodeSwayamSDK } from '@codeswayam/api-client';
const sdk = new CodeSwayamSDK({
baseUrl: process.env.NEXT_PUBLIC_API_URL,
onUnauthorized: () => redirectToLogin(),
});
// Auth
const profile = await sdk.auth.getProfile();
// Billing
const subs = await sdk.billing.getSubscriptions();
// Credits
const wallet = await sdk.credits.getWallet();
// Entitlements
const canUse = sdk.entitlements.canAccess('smart_ai', activeSubs);
const remaining = sdk.entitlements.getRemainingLimit('max_automations', 5, activeSubs);
// Full profile in one call
const { profile, subscriptions, wallet } = await sdk.getFullProfile();Auth Functions
import { getSession, fetchProfile, updateProfile, logout } from '@codeswayam/api-client';
const user = await getSession(); // { id, email, name, access_scopes }
const profile = await fetchProfile();
await updateProfile({ name: 'New Name' });
await logout();Subscription Functions
import {
fetchPublicPlans,
fetchUserSubscriptions,
fetchSubscriptionById,
createRazorpayOrder,
verifyRazorpayPayment,
changeSubscriptionPlan,
cancelUserSubscription,
} from '@codeswayam/api-client';
// Public plans (no auth needed)
const { products, bundles } = await fetchPublicPlans();
// User's subscriptions
const subs = await fetchUserSubscriptions();
const sub = await fetchSubscriptionById(123);
// Payment — pass returnUrl for cross-domain redirect, and optional upgradeFromSubscriptionId
const order = await createRazorpayOrder({
saasProductId: 1,
billingCycle: 'monthly',
currency: 'INR',
returnUrl: `${window.location.origin}/dashboard`,
upgradeFromSubscriptionId: 123, // Optional: old subscription ID to upgrade from (prorates cost)
});
await verifyRazorpayPayment({
razorpay_order_id: order.orderId,
razorpay_payment_id: paymentId,
razorpay_signature: signature,
saasProductId: 1,
billingCycle: 'monthly',
currency: 'INR',
amount: order.amount,
upgradeFromSubscriptionId: 123, // Pass old subscription ID to automatically cancel it upon payment success
});
// Upgrade/downgrade
await changeSubscriptionPlan(subId, {
saasProductId: 2,
billingCycle: 'yearly',
currency: 'INR',
});
await cancelUserSubscription(subId);Credits Functions
import {
fetchMyWallet,
fetchCreditPacks,
fetchFeatureCosts,
createCreditPurchaseOrder,
verifyCreditPurchase,
useCredits,
} from '@codeswayam/api-client';
const { wallet, transactions } = await fetchMyWallet();
const packs = await fetchCreditPacks();
const costs = await fetchFeatureCosts('auraflow');
// Deduct credits (server-side)
await useCredits({ saasId: 'auraflow', featureKey: 'ai_chat', quantity: 1 });Cross-Domain Auth
All functions automatically inject the Bearer token from localStorage for cross-domain compatibility:
import { getAuthHeaders } from '@codeswayam/api-client';
// Use in any custom fetch call
const headers = getAuthHeaders();
fetch(`${apiUrl}/my-endpoint`, { headers });Referrals & Coupons
import { fetchReferralStats, redeemReferralCode, redeemCouponCode } from '@codeswayam/api-client';
const stats = await fetchReferralStats();
await redeemReferralCode('FRIEND123');
await redeemCouponCode('LAUNCH50');Push Notifications
import { registerPushSubscription, fetchVapidPublicKey } from '@codeswayam/api-client';
const { publicKey } = await fetchVapidPublicKey();
const sub = await navigator.serviceWorker.ready.then(sw =>
sw.pushManager.subscribe({ userVisibleOnly: true, applicationServerKey: publicKey })
);
await registerPushSubscription(sub.toJSON(), 'auraflow');