npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@dream-api/sdk

v0.1.1

Published

Official SDK for Dream API - Auth, billing, and usage tracking in one API

Readme

@dream-api/sdk

Official SDK for Dream API - Auth, billing, and usage tracking in one API.

Installation

npm install @dream-api/sdk

Quick Start

import { DreamAPI } from '@dream-api/sdk';

const api = new DreamAPI({
  secretKey: process.env.DREAM_API_SECRET_KEY,
  publishableKey: process.env.DREAM_API_PUBLISHABLE_KEY,
});

Backend Operations (SK Only)

These operations only require your secret key:

Create Customer

const { customer } = await api.customers.create({
  email: '[email protected]',
  firstName: 'John',
  plan: 'free',
});

Delete Customer

await api.customers.delete(customerId);

Get Dashboard Metrics

const dashboard = await api.dashboard.get();
console.log(`MRR: $${dashboard.mrr}`);
console.log(`Active subs: ${dashboard.activeSubscriptions}`);

List Products/Tiers

const { tiers } = await api.products.listTiers();
const { products } = await api.products.list();

Frontend Operations (Requires User Token)

After user signs in via Clerk, set the token:

// Get token from Clerk
const token = await clerk.session.getToken();
api.setUserToken(token);

Track Usage

const { usage } = await api.usage.track();
console.log(`Used ${usage.used} of ${usage.limit}`);

Check Usage

const usage = await api.usage.check();
if (usage.remaining <= 0) {
  // Show upgrade prompt
}

Create Checkout (Subscription Upgrade)

const { url } = await api.billing.createCheckout({
  tier: 'pro',
  successUrl: 'https://yourapp.com/success',
  cancelUrl: 'https://yourapp.com/pricing',
});
window.location.href = url;

Open Customer Portal

const { url } = await api.billing.openPortal({
  returnUrl: 'https://yourapp.com/dashboard',
});
window.location.href = url;

Auth URL Helpers

Sign Up URL

const signupUrl = api.auth.getSignUpUrl({
  redirect: 'https://yourapp.com/dashboard',
});

// Use in your app
<a href={signupUrl}>Sign Up</a>

Sign In URL

After initial signup, users sign in via your Clerk instance directly.

Store/E-commerce

Cart Checkout (Guest)

const { url } = await api.products.cartCheckout({
  items: [
    { priceId: 'price_xxx', quantity: 2 },
    { priceId: 'price_yyy', quantity: 1 },
  ],
  customerEmail: '[email protected]',
  customerName: 'Jane Doe',
  successUrl: 'https://yourapp.com/success',
  cancelUrl: 'https://yourapp.com/cart',
});

Error Handling

import { DreamAPIException } from '@dream-api/sdk';

try {
  await api.usage.track();
} catch (error) {
  if (error instanceof DreamAPIException) {
    if (error.status === 403) {
      // Usage limit exceeded
      console.log('Upgrade required');
    } else if (error.status === 401) {
      // Token expired or invalid
      console.log('Please sign in again');
    }
  }
}

TypeScript

Full TypeScript support with exported types:

import type {
  Customer,
  Usage,
  Tier,
  DashboardMetrics,
  DreamAPIConfig,
} from '@dream-api/sdk';

Environment Variables

DREAM_API_SECRET_KEY=sk_test_xxx
DREAM_API_PUBLISHABLE_KEY=pk_test_xxx

Framework Examples

React

import { DreamAPI } from '@dream-api/sdk';
import { useAuth } from '@clerk/clerk-react';

const api = new DreamAPI({
  secretKey: import.meta.env.VITE_DREAM_API_SECRET_KEY,
  publishableKey: import.meta.env.VITE_DREAM_API_PUBLISHABLE_KEY,
});

function Dashboard() {
  const { getToken } = useAuth();
  const [usage, setUsage] = useState(null);

  useEffect(() => {
    async function loadUsage() {
      const token = await getToken();
      api.setUserToken(token);
      const data = await api.usage.check();
      setUsage(data);
    }
    loadUsage();
  }, []);

  return <div>Used: {usage?.used} / {usage?.limit}</div>;
}

Next.js (API Route)

// app/api/track/route.ts
import { DreamAPI } from '@dream-api/sdk';

const api = new DreamAPI({
  secretKey: process.env.DREAM_API_SECRET_KEY!,
});

export async function POST(request: Request) {
  const token = request.headers.get('Authorization')?.replace('Bearer ', '');
  api.setUserToken(token!);

  const result = await api.usage.track();
  return Response.json(result);
}

Support

  • Documentation: https://docs.dream-api.com
  • Issues: https://github.com/dream-api/sdk/issues