OpenRouter Client

CI CD

A Dart client library for the OpenRouter API.

Features

  • List available models.
  • Create chat completions.
  • Stream chat completions.
  • Generate images.
  • Get remaining credits (management key).
  • Tool/function calling via tools.
  • Pass through extra OpenRouter parameters as needed.

Advantages

  • Easy to pick up with a small, familiar API surface.
  • Type-safe request/response models for fewer runtime surprises.
  • Lightweight and dependency-friendly for faster builds.
  • Designed for reuse with a single client instance.
  • Flexible extras let you access new OpenRouter options without waiting on updates.

Getting started

Add the dependency to your pubspec:

dependencies:
	openrouter_client: ^0.1.0

Usage

Create the client once and reuse it across calls:

import 'package:openrouter_client/openrouter_client.dart';

final client = OpenRouterClient(apiKey: 'YOUR_API_KEY');

List models

final models = await client.listModels();
final firstModel = models.data.first.id;
print(firstModel);

Chat completion

final response = await client.createChatCompletion(
	ChatCompletionRequest(
		model: 'openai/gpt-4o-mini',
		messages: [
			ChatMessage(role: 'user', content: 'Say hello from OpenRouter'),
		],
		temperature: 0.7,
	),
);

print(response);

Chat completion stream

final stream = client.streamChatCompletion(
	ChatCompletionRequest(
		model: 'openai/gpt-4o-mini',
		messages: [
			ChatMessage(role: 'user', content: 'Write a short poem about rain.'),
		],
	),
);

await for (final chunk in stream) {
	final delta = chunk.choices.first.delta.content;
	if (delta != null) {
		print(delta);
	}
}

Image generation

final image = await client.generateImage(
	ImageGenerationRequest(
		model: 'openai/gpt-image-1',
		prompt: 'A photoreal red panda astronaut floating in space',
		outputFormat: 'png',
		n: 1,
	),
);

final base64Image = image.data.first.b64Json;
print(base64Image);

Remaining credits

final credits = await client.getRemainingCredits();

print('Total credits: ${credits.data.totalCredits}');
print('Total usage: ${credits.data.totalUsage}');
print('Remaining: ${credits.data.remainingCredits}');

Tools (function calling)

final response = await client.createChatCompletion(
	ChatCompletionRequest(
		model: 'openai/gpt-4o-mini',
		messages: [
			ChatMessage(role: 'user', content: 'What is the weather in Tokyo?'),
		],
		tools: [
			ToolDefinition(
				function: ToolFunctionDefinition(
					name: 'get_weather',
					description: 'Get weather by city name',
					parameters: {
						'type': 'object',
						'properties': {
							'city': {'type': 'string'},
						},
						'required': ['city'],
					},
				),
			),
		],
		toolChoice: ToolChoice.auto(),
	),
);

final toolCalls = response.choices.first.message.toolCalls ?? [];
for (final call in toolCalls) {
	print(call.function?.name);
	print(call.function?.arguments);
}

Extra parameters

final response = await client.createChatCompletion(
	ChatCompletionRequest(
		model: 'openai/gpt-4o-mini',
		messages: [
			ChatMessage(role: 'user', content: 'Give me one fun fact about space.'),
		],
		extra: {
			'parallel_tool_calls': true,
			'max_completion_tokens': 64,
		},
	),
);

print(response.choices.first.message.content);

Test coverage

Generate test coverage and HTML output locally with:

./scripts/flutter_coverage.sh

The script will:

  • Run flutter test --coverage.
  • Generate HTML output using genhtml from coverage/lcov.info.
  • Save genhtml command output to coverage/output.txt.

If genhtml is missing, install it with:

brew install lcov

Additional information

CI/CD

This repository now includes GitHub Actions workflows for continuous integration and delivery:

  • CI workflow (.github/workflows/ci.yml)

    • Triggers on pushes to main and pull requests.
    • Uses a Dart-only dependency cache keyed to root pubspec.yaml.
    • Installs Flutter for the example/ app and runs flutter pub get --directory example.
    • Runs dart pub get, static analysis, tests, and generates LCOV from VM coverage.
    • Uploads coverage/lcov.info as an artifact.
  • CD workflow (.github/workflows/cd.yml)

    • Triggers on version tags matching v*.*.* (for example v0.1.1) and manual dispatch.
    • Uses a Dart-only dependency cache keyed to root pubspec.yaml.
    • Re-runs formatting, analysis, and tests before publishing.
    • Publishes to pub.dev using dart pub publish --force.

Libraries

openrouter_client