package openrouter_api
Install
dune-project
Dependency
Authors
Maintainers
Sources
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.
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_apiQuickstart
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 fmtResponse 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.
Dependencies (10)
- jsonaf
- core
-
async_ssl
>= "v0.17.0-1" - cohttp-async
- async
- ppx_typed_fields
- ppx_jane
- ppx_jsonaf_conv
-
ocaml
>= "5.2.0" -
dune
>= "3.22"
Used by
None
Conflicts
None