package openrouter_api

  1. Overview
  2. Docs
OCaml client for the OpenRouter API

Install

dune-project
 Dependency

Authors

Maintainers

Sources

v0.1.0.tar.gz
md5=37c701d570788095f9a0690e401a4b0c
sha512=0f6e6d347094aff4efb50330d50ade3f1706929400498998a8d8f409f1ab45099d792cc469210e53c2ca06b0e1a9b20fdf8e89361a52969cc495812092fe584b

Description

An Async-based OCaml client for OpenRouter (https://openrouter.ai), a unified API for LLMs from multiple providers (Anthropic, OpenAI, Google, and others). Covers chat completions (with streaming, tools, plugins, multimodal content, provider routing, reasoning, and structured outputs), text embeddings, model listing, and generation stats.

Tags

openrouter llm ai api-client async

Added to opam-repository:

README

openrouter-ocaml

An Async-based OCaml client for the OpenRouter API. Full API docs: https://mt-caret.github.io/openrouter-ocaml/.

opam install openrouter_api

Quickstart

open! Core
open! Async
open Openrouter_api

let run_once ~api_key =
  let request =
    Completions.Request.create
      ~model:"openai/gpt-4o-mini"
      ~messages:[ Completions.Request.Message.user "Say pong." ]
      ()
  in
  Completions.create ~api_key request
  >>| function
  | Error error -> eprintf "%s\n" (Error.to_string_hum error)
  | Ok (response : Completions.Response.t) ->
    response.choices
    |> List.iter ~f:(fun (choice : Completions.Response.Choice.t) ->
      Option.iter choice.message.content ~f:print_endline)
;;

Feature Coverage

OpenRouter functionality

Supported

Chat completions

Yes

Streaming chat completions

Yes

Model listing

Yes

Generation stats

Yes

Embeddings

Yes

Sampling and output controls

Yes

Provider routing

Yes

Prompt caching

Yes

Reasoning controls and responses

Yes

Structured outputs

Yes

Tool calling

Yes

OpenRouter server tools

Yes

Plugins

Yes

Multimodal inputs

Yes

Image outputs

Yes

Audio outputs

Yes

Credits endpoint

No

API key management

No

Organization/workspace management

No

Guardrails

No

Reranking

No

Audio transcription/speech endpoints

No

Video generation endpoints

No

Anthropic Messages API

No

Responses API

No

Example CLI

export OPENROUTER_API_KEY=sk-or-v1-...
dune exec example/openrouter_api_example.exe -- help -recursive -flags

dune exec example/openrouter_api_example.exe -- chat \
  -no-stream \
  -model openai/gpt-4o-mini \
  "say 'pong'"

The full flag reference is generated in example/help.txt.

Development

After substantive edits, run:

dune build
dune runtest
dune fmt

Response fixtures live in test/responses/; request-wire snapshots live in test/test_request_serialization.ml. Use dune runtest --auto-promote to refresh expect tests, then inspect the diff before keeping promoted snapshots.

Dev Dependencies (2)

  1. odoc with-doc
  2. mdx with-test & >= "2.3.0"

Used by

None

Conflicts

None