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

opencode-openrouter-auth-broker-plugin

v0.2.0

Published

OpenCode plugin for custom broker-managed OpenRouter credentials

Readme

OpenCode OpenRouter Auth Broker Plugin

Generic OpenCode plugin for broker-managed OpenRouter credentials.

The plugin registers a native OpenCode auth provider. Users authenticate through OpenCode's existing provider login flow; the broker performs browser-based OpenRouter auth, creates a managed OpenRouter API key, and returns it to OpenCode for storage in OpenCode's normal auth store.

Broker Contract

The broker is expected to implement:

  • GET /auth/openrouter/start?return_to=http://127.0.0.1:<port>/callback
  • POST /auth/openrouter/credential with { "code": "..." }
  • POST /credentials/rotate with Authorization: Bearer <broker_refresh_token>

The credential exchange response must include:

{
  "openrouter_api_key": "sk-or-v1-...",
  "broker_refresh_token": "...",
  "openrouter_key_hash": "...",
  "openrouter_key_label": "...",
  "openrouter_key_expires_at": "..."
}

OpenCode Config

{
  "plugin": [
    [
      "opencode-openrouter-auth-broker-plugin",
      {
        "providerID": "openrouter-broker",
        "brokerUrl": "https://your-broker.example.com",
        "envName": "OPENROUTER_API_KEY"
      }
    ]
  ],
  "provider": {
    "openrouter-broker": {
      "name": "OpenRouter Broker",
      "npm": "@ai-sdk/openai-compatible",
      "options": {
        "baseURL": "https://openrouter.ai/api/v1"
      },
      "models": {
        "openai/gpt-5.5": {
          "name": "GPT-5.5"
        }
      }
    }
  }
}

Login

opencode providers login --provider openrouter-broker --method "Browser sign-in"

OpenCode will print/open the browser auth URL and wait for the localhost callback. On success it stores the returned OpenRouter API key as the provider credential in OpenCode's normal auth store.

The plugin opens the browser automatically by default when auth starts. Set plugin option openBrowser: false or environment variable OPENCODE_OPENROUTER_AUTH_NO_OPEN=1 to only print the URL.

The local callback server uses an ephemeral port by default, so it should not collide with common development ports. Set authPort only if your broker requires a fixed callback port.

For deployments where the broker requires identity headers during auth-start, configure startHeaders in plugin options. This is intended for controlled enterprise/MDM environments.

Optional Auto-login

Set autoLogin: true to have the plugin start the same browser auth flow automatically on the first model request when no usable provider credential is present.

{
  "providerID": "openrouter-broker",
  "brokerUrl": "https://your-broker.example.com",
  "autoLogin": true
}

Revoked Key Handling

On provider load, the plugin validates the stored OpenRouter key. If OpenRouter rejects it and a broker_refresh_token is available in auth metadata, the plugin rotates the key through the broker before the model request is sent. Set validateOnLoad: false to skip this validation call.

If an inference request still reaches OpenRouter with an expired or revoked stored key, the plugin traps a 401 response. It first re-reads OpenCode auth and retries with a newer stored key if another OpenCode instance already refreshed it. If auth still contains the stale key, it rotates credentials through the broker, saves the replacement key to OpenCode auth, and retries the request once.

Web Search

The plugin can give your session model inline web search backed by OpenRouter's (non-deprecated) openrouter:web_search server tool. It works like Claude Code's WebSearch: the capability is offered on every turn, but the model only searches when it decides it needs current information, and the search runs inside the model's own completion with full conversation context — no second model call, no separate search vendor, no telemetry.

Mechanism: the plugin's outbound fetch wrapper appends { "type": "openrouter:web_search", ... } to the tools array of each /chat/completions request for this provider. OpenRouter executes the search server-side under the broker-managed key and returns the grounded answer with url_citation annotations. Injection is best-effort and fails open — it never breaks an inference request.

It is enabled by default. Disable it with webSearch: false.

{
  "plugin": [
    [
      "opencode-openrouter-auth-broker-plugin",
      {
        "providerID": "openrouter-broker",
        "brokerUrl": "https://your-broker.example.com",
        "webSearch": true,
        "webSearchEngine": "auto",
        "webSearchMaxResults": 8
      }
    ]
  ]
}

Web search options (all optional):

| Option | Default | Description | | ------------------------ | -------- | --------------------------------------------------------------------------------------------------------------------------------- | | webSearch | true | Set false to not inject the web search server tool. | | webSearchEngine | "auto" | auto | native | exa | firecrawl | parallel | perplexity. auto uses native provider search, falling back to Exa. | | webSearchMaxResults | 8 | Max results per search (1–25). | | webSearchMaxCharacters | unset | Optional per-result character cap forwarded to OpenRouter. |

The search runs through OpenRouter under the broker-managed key. With webSearchEngine: "auto", native-search-capable providers (Anthropic, OpenAI, Google, …) serve the search themselves; models without native search fall back to Exa via OpenRouter's server-side key, never an unauthenticated consumer search endpoint.

Visibility note: because the search is a server-side tool executed inside the completion, OpenCode does not render it as a separate tool-call line. Grounding surfaces as inline citations in the model's answer rather than a visible "web search" step.

Publishing

This package publishes through GitHub Actions trusted publishing from .github/workflows/publish.yml.

For maintainer release steps and verification, use:

  • .claude/skills/publish-package/SKILL.md