Skip to content

credits.getCredits() returns {} because GetCreditsResponse schema discards API fields #96

@gwenphalan

Description

@gwenphalan

The /credits HTTP endpoint returns credit data as expected, but the TypeScript SDK’s credits.getCredits() method returns an effectively empty object because the Zod schema is defined as {}.

What I’m seeing

Direct HTTP call works:

curl https://openrouter.ai/api/v1/credits \
  -H "Authorization: Bearer sk-xxxx"

Response:

{
  "data": {
    "total_credits": 10,
    "total_usage": 0
  }
}

But using the SDK:

import { OpenRouter } from "@openrouter/sdk";

const client = new OpenRouter({ apiKey: process.env.OPENROUTER_API_KEY! });

const result = await client.credits.getCredits();
console.log(result); // {}

The logged result is just {} (or an essentially empty object), with none of the data.total_credits / data.total_usage fields from the API.

Likely cause

In the published typings:

// node_modules/@openrouter/sdk/esm/models/operations/getcredits.d.ts

/**
 * Total credits purchased and used
 */
export type GetCreditsResponse = {};

And the inbound Zod schema is defined against that empty shape, so the response body is parsed into {} and all fields from the JSON payload are discarded before being returned by credits.getCredits().

Meanwhile, the generated request code clearly expects a JSON body for 200 responses:

// node_modules/@openrouter/sdk/esm/funcs/creditsGetCredits.js
const [result] = await M.match(
  M.json(200, operations.GetCreditsResponse$inboundSchema),
  ...
)(response, req, { extraFields: responseFields });

Because GetCreditsResponse$inboundSchema is an empty object schema, the actual fields are never surfaced to the caller.

Expected behavior

client.credits.getCredits() should return a typed object that exposes the same data fields the HTTP API returns, e.g.:

type GetCreditsResponse = {
  data: {
    total_credits: number;
    total_usage: number;
    // any other documented fields
  };
};

Then:

const result = await client.credits.getCredits();
console.log(result.data.total_credits); // 10
console.log(result.data.total_usage);   // 0

Actual behavior

  • The API response has the expected data under data.
  • The SDK’s credits.getCredits() returns an object typed as {} (and effectively empty at runtime when inspected).

Environment

Suggested fix

  • Update GetCreditsResponse and its Zod inbound schema to match the actual JSON structure returned by /credits (at least including data.total_credits and data.total_usage).
  • Optionally add proper types for the data object so SDK users can rely on them instead of casting to any or hitting the HTTP endpoint manually.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions