Client

NAME

OpenRouter::API::Client - HTTP client for OpenRouter's non-inference API

SYNOPSIS


use OpenRouter::API::Client;

# Reads OPENROUTER_API_KEY from env by default.
my $or = OpenRouter::API::Client.new;

# Explicit key + attribution — shown on OR's rankings page / logs.
my $attr = OpenRouter::API::Client.new(
    :api-key(%*ENV<OPENROUTER_API_KEY>),
    :http-referer<https://example.com/my-tool>,
    :x-title<My Tool>,
);

# No-auth endpoints work without a key.
my $anon = OpenRouter::API::Client.new;
my @all  = $anon.get-models;   # /models is public

DESCRIPTION

Owns all HTTP I/O to https://openrouter.ai/api/v1. Public methods are the per-endpoint wrappers (get-models, get-credits, get-activity, get-generation, ...). The OpenRouter::API-level module simply re-exports this class along with the OpenRouter::API::Filter helper and each Result::* type.

Internals:

  • Private !request builds the URL (base-url + path + query), adds auth + attribution headers, calls Cro::HTTP::Client.get, parses JSON, and normalises any failure into a die with an OpenRouter::API: prefix.

  • Per-call Cro::HTTP::Client.new — no connection pool, matches the established monorepo wrapper convention.

  • Models catalogue cached in-process with a 5-minute TTL via %!cached-models. Disabled by :cache(False); invalidate with .clear-cache. Other endpoints never touch the cache.

ATTRIBUTES

  • $.api-key — OpenRouter Bearer token. Defaults to %*ENV<OPENROUTER_API_KEY> via the TWEAK submethod. May stay undefined for public endpoints; methods that need a key die with a helpful message.

  • $.base-url — overrideable for regional endpoints or mock servers. Defaults to https://openrouter.ai/api/v1.

  • $.http-referer — optional HTTP-Referer attribution.

  • $.x-title — optional X-Title attribution.

  • $.cache — whether search-models caches the models catalogue. Default True.

  • $.cache-ttl — cache lifetime. Default 5 minutes.

OpenRouter::API v0.1.1

Raku client for OpenRouter's non-inference REST API

Authors

  • Matt Doughty

License

Artistic-2.0

Dependencies

Cro::HTTP:ver<0.8.11>:auth<zef:cro>JSON::Fast:ver<0.19>:auth<cpan:TIMOTIMO>

Test Dependencies

Provides

  • OpenRouter::API
  • OpenRouter::API::Client
  • OpenRouter::API::Filter
  • OpenRouter::API::Result::ActivityRow
  • OpenRouter::API::Result::Credits
  • OpenRouter::API::Result::Endpoint
  • OpenRouter::API::Result::Generation
  • OpenRouter::API::Result::Model
  • OpenRouter::API::Result::Provider

The Camelia image is copyright 2009 by Larry Wall. "Raku" is a trademark of the Yet Another Society. All rights reserved.

Built with Podlite — the markup and publishing tools behind this site.