OpenRouterClient.ts
OpenRouterClient.ts overview
Section titled “OpenRouterClient.ts overview”The OpenRouterClient module provides an Effect service for calling
OpenRouter’s chat completions API. It wraps the generated OpenRouter HTTP
client with Effect-native constructors, layers, authentication and optional
site ranking headers, typed errors, and streaming support.
Since v4.0.0
Exports Grouped by Category
Section titled “Exports Grouped by Category”constructors
Section titled “constructors”Creates an OpenRouter client service from explicit options.
When to use
Use when you need the OpenRouter client service value inside an effect.
Details
The returned service uses the current HttpClient, prepends apiUrl or
https://openrouter.ai/api/v1, adds the bearer token and optional
HTTP-Referer and X-Title headers, accepts JSON responses, and applies
transformClient when provided.
Gotchas
Scoped OpenRouterConfig.withClientTransform applies to generated client
request methods. Streaming chat completion requests are sent directly by this
module and do not read that scoped transform.
See
layerfor providing this client from explicit optionslayerConfigfor loading client settings fromConfig
Signature
declare const make: (options: Options) => Effect.Effect<Service, never, HttpClient.HttpClient>Since v4.0.0
layers
Section titled “layers”Creates a layer for the OpenRouter client with the given options.
When to use
Use when you already have the OpenRouter client options in code and want to
provide OpenRouterClient as a layer.
See
makefor constructing the client service effectfullylayerConfigfor loading client settings fromConfig
Signature
declare const layer: (options: Options) => Layer.Layer<OpenRouterClient, never, HttpClient.HttpClient>Since v4.0.0
layerConfig
Section titled “layerConfig”Creates a layer for the OpenRouter client from provided Config values.
When to use
Use when you need client settings for OpenRouter to be read from Effect
Config values while providing OpenRouterClient as a layer.
Details
Only config values supplied in options are loaded. Omitted fields are
passed to make as undefined, and transformClient is forwarded as a
plain option.
See
makefor constructing the client service effectfullylayerfor providing the client from already-resolved options
Signature
declare const layerConfig: (options?: { readonly apiKey?: Config.Config<Redacted.Redacted<string> | undefined> | undefined readonly apiUrl?: Config.Config<string> | undefined readonly siteReferrer?: Config.Config<string> | undefined readonly siteTitle?: Config.Config<string> | undefined readonly transformClient?: ((client: HttpClient.HttpClient) => HttpClient.HttpClient) | undefined}) => Layer.Layer<OpenRouterClient, Config.ConfigError, HttpClient.HttpClient>Since v4.0.0
models
Section titled “models”ChatStreamingResponseChunkData (type alias)
Section titled “ChatStreamingResponseChunkData (type alias)”Decoded data payload from an OpenRouter chat completion streaming chunk.
Details
The payload contains streamed choices, model metadata, optional usage, and may include an OpenRouter error object for a streamed response.
Signature
type ChatStreamingResponseChunkData = typeof Generated.ChatStreamingResponseChunk.fields.data.TypeSince v4.0.0
Service (interface)
Section titled “Service (interface)”The OpenRouter client service interface.
Details
Provides methods for interacting with OpenRouter’s Chat Completions API, including both synchronous and streaming message creation.
Signature
export interface Service { readonly client: Generated.OpenRouterClient
readonly createChatCompletion: ( options: typeof Generated.ChatGenerationParams.Encoded ) => Effect.Effect< [body: typeof Generated.SendChatCompletionRequest200.Type, response: HttpClientResponse.HttpClientResponse], AiError.AiError >
readonly createChatCompletionStream: ( options: Omit<typeof Generated.ChatGenerationParams.Encoded, "stream" | "stream_options"> ) => Effect.Effect< [ response: HttpClientResponse.HttpClientResponse, stream: Stream.Stream<ChatStreamingResponseChunkData, AiError.AiError> ], AiError.AiError >}Since v4.0.0
options
Section titled “options”Options (type alias)
Section titled “Options (type alias)”Configuration for creating an OpenRouter client.
Signature
type Options = { readonly apiKey?: Redacted.Redacted<string> | undefined
readonly apiUrl?: string | undefined
/** * Optional URL of your site for rankings on `openrouter.ai`. */ readonly siteReferrer?: string | undefined
/** * Optional title of your site for rankings on `openrouter.ai`. */ readonly siteTitle?: string | undefined
/** * Optional transformer for the underlying HTTP client. * * **When to use** * * Use to add middleware, logging, or custom request/response handling. */ readonly transformClient?: ((client: HttpClient.HttpClient) => HttpClient.HttpClient) | undefined}Since v4.0.0
services
Section titled “services”OpenRouterClient (class)
Section titled “OpenRouterClient (class)”Service tag for the OpenRouter client.
When to use
Use when accessing or providing the OpenRouter client service through Effect’s context.
See
makefor constructing an OpenRouter client effectfullylayerfor providing a client from explicit optionslayerConfigfor providing a client fromConfig
Signature
declare class OpenRouterClientSince v4.0.0