cred.diy/ docs
    Sign inGet an API key
    All documentation

    SDK Installation - cred.diy API documentation

    SDK Installation

    The CRED SDK makes it easy to integrate credit systems into your TypeScript/JavaScript applications. Install the SDK and start managing credits in minutes.

    Installation

    npm install @cred/sdk
    # or
    yarn add @cred/sdk
    # or
    pnpm add @cred/sdk

    Initialize the Client

    Create a CRED client instance with your API key and project ID.

    import { CredClient } from '@cred/sdk';
    
    // Initialize the client
    const cred = new CredClient({
      apiKey: process.env.CRED_API_KEY,
      projectId: 'your_project_id',
      // Which of your apps these calls come from. Sent as x-app-origin and used
      // for signup attribution. In the browser it defaults to the page origin;
      // on a server, set it — otherwise attribution stays blank.
      appOrigin: 'https://app1.example.com'
    });

    SDK Features

    cred.credits - Issue, deduct, check balances
    cred.pricing - Manage pricing tiers
    cred.users - User management
    cred.checkout - Create checkout sessions

    Issue Credits

    // Issue credits to a user
    const result = await cred.credits.issue({
      user_id: 'user_123',
      amount: 100,
      reason: 'Welcome bonus'
    });
    
    if (result.success) {
      console.log('New balance:', result.data.balance);
    }

    Deduct Credits

    // Deduct credits from a user
    const result = await cred.credits.deduct({
      user_id: 'user_123',
      amount: 10,
      reason: 'API usage'
    });
    
    if (!result.success) {
      console.error('Error:', result.error?.message);
    }

    Check Balance

    // Get user's credit balance
    const balance = await cred.credits.getBalance({
      user_id: 'user_123'
    });
    
    console.log('Balance:', balance.data?.balance);

    Manage Pricing Tiers

    // List all pricing tiers
    const tiers = await cred.pricing.list();
    tiers.data?.forEach(tier => {
      console.log(`${tier.name}: ${tier.credits} credits for $${tier.price}`);
    });
    
    // Create a new pricing tier
    const newTier = await cred.pricing.create({
      name: 'Starter Pack',
      description: 'Perfect for getting started',
      price: 9.99,
      credits: 100,
      currency: 'USD'
    });

    Create Checkout Sessions

    // Create a checkout session
    const session = await cred.checkout.create({
      pricing_tier_id: 'tier_123',
      user_id: 'user_456',
      success_url: 'https://myapp.com/success',
      cancel_url: 'https://myapp.com/cancel'
    });
    
    // Redirect user to checkout
    if (session.success) {
      window.location.href = session.data.checkout_url;
    }

    User Management

    // Create or get a user
    const user = await cred.users.createOrGet({
      external_id: 'auth0|123456',
      email: 'user@example.com',
      name: 'John Doe'
    });
    
    // Get user balances
    const balances = await cred.users.getBalances('auth0|123456');

    TypeScript Support

    The SDK is fully typed. Import types like CreditBalance,PricingTier,EndUser, and more from@cred/sdk.

    Your first credit is one request away.

    Create a project, copy an API key, and issue a credit from your server.