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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@cqaiclub/cqai-account-sdk

v0.1.1

Published

Server-side TypeScript SDK for NewAPI login, account provisioning, tokens, usage, and billing

Downloads

206

Readme

CQAI Account SDK

Server-side TypeScript clients for the NewAPI HTTP interface. NewAPI remains the source of truth for dashboard users, API keys, quota, usage, and billing. This package does not contain an identity provider integration or a local database.

Install

npm install @cqaiclub/cqai-account-sdk

Before the first npm release, install directly from GitHub. The package's prepare script builds dist during installation:

npm install github:cqai-club/cqai-account-sdk

Dashboard login and account queries

import { NewApiAuthClient } from '@cqaiclub/cqai-account-sdk'

const auth = new NewApiAuthClient({
  baseUrl: process.env.NEW_API_URL!,
  // Keep the access token and refresh cookie on a trusted backend.
  origin: process.env.NEW_API_ORIGIN,
  credentials: 'include',
})

const login = await auth.login({
  username: 'alice',
  password: process.env.NEW_API_PASSWORD!,
})

if (login.kind === 'two_factor') {
  const authenticated = await auth.login2FA(login.flowToken, getTotpCode())
  console.log(authenticated.user?.username)
} else {
  console.log(login.user?.username)
}

const user = await auth.getSelf()
const tokens = await auth.listTokens({ page: 1, pageSize: 20 })
const fullKey = await auth.getTokenKey(tokens.items[0].id)

// refresh() uses the HttpOnly new_api_refresh cookie captured by the client.
await auth.refresh()
await auth.logout()

login() returns a discriminated union. A successful password login stores the dashboard access token and refresh cookie in the client. When Node's fetch cannot expose Set-Cookie, pass a custom cookieJar or use a same-origin browser request with credentials: 'include'.

The login endpoint may require a Cloudflare Turnstile token. Pass it as the second argument: auth.login(request, { turnstile }). If password encryption is enabled in NewAPI, provide the caller-encrypted fields passwordEncrypted and encryptionKeyId; the SDK does not handle private RSA keys.

Service provisioning and relay usage

Provisioning is server-only and requires the protected NewAPI service token:

import { NewApiClient } from '@cqaiclub/cqai-account-sdk'

const newApi = new NewApiClient({
  baseUrl: process.env.NEW_API_URL!,
  serviceToken: process.env.NEW_API_INTERNAL_TOKEN!,
})

const binding = await newApi.provision({
  issuer: 'https://accounts.example.com',
  subject: 'user-123',
  platform: 'writer-app',
}, { idempotencyKey: 'writer-app:user-123' })

if (binding.apiKey) {
  // Store the key only in the platform backend.
  const usage = await newApi.getTokenUsage(binding.apiKey)
  const subscription = await newApi.getSubscription(binding.apiKey)
  const billing = await newApi.getBillingUsage(binding.apiKey)
  console.log(usage.totalAvailable, subscription.accessUntil, billing.totalUsage)
}

NEW_API_INTERNAL_TOKEN must exactly match the value configured on NewAPI. Use a long random value and never expose it through a VITE_* variable or a browser bundle.

getTokenUsage(), getSubscription(), and getBillingUsage() use a relay API key. They must not be called with a dashboard access token or the service token. The token list endpoint intentionally returns a masked key; call getTokenKey(id) only on a trusted backend when the full key is required.

Provisioning creates or reuses the NewAPI user/key binding. NewAPI-generated provisioning users do not return a dashboard password, so provisioning is not a replacement for a user login flow. Existing users can use login() with their NewAPI credentials, or the product can keep its own login and use provisioning server-to-server.

API surface

  • NewApiAuthClient: encryption-key lookup, login, 2FA, registration, refresh, logout, current-user lookup, token CRUD, and full-key retrieval.
  • NewApiClient: protected provisioning plus relay-key usage and billing.
  • MemoryCookieJar: small single-origin cookie jar for Node server processes.
  • AiAccountError: normalized HTTP/business error with status, code, and optional retryAfter.

Do not put serviceToken, dashboard access tokens, user passwords, or full API keys in browser bundles or logs. For browser applications, use a same-origin backend proxy when possible; cross-origin refresh depends on cookie, CORS, and trusted-origin configuration in NewAPI.

Development

npm run typecheck
npm run build