Browse documentation

Dart & Flutter SDK

The official nRouter Dart & Flutter SDK with cross-platform support for web, mobile, and desktop, Server-Sent Events streaming, and typed error handling.

The official nRouter Dart SDK (nrouter) provides cross-platform client bindings for Flutter and Dart VM applications connecting to https://api.nrouter.ai/v1. Configure NROUTER_API_KEY authentication, execute asynchronous chat completions, consume SSE token streams, and handle sealed gateway error types across mobile, web, and desktop.

Installation

dart pub add nrouter
# Flutter applications:
flutter pub add nrouter

Chat Completion

Set your key via environment or pass it directly on mobile:

export NROUTER_API_KEY="sk-nrouter-xxxxxxxxxxxx"
import 'package:nrouter/nrouter.dart';

Future<void> main() async {
  const apiKey = String.fromEnvironment(
    'NROUTER_API_KEY',
    defaultValue: 'sk-nrouter-xxxxxxxxxxxx',
  );
  final client = NRouter(
    apiKey: apiKey,
    baseUrl: 'https://api.nrouter.ai/v1',
  );

  final result = await client.chatCompletions({
    'model': 'gpt-5.4-mini',
    'messages': [{'role': 'user', 'content': 'Hello, nRouter!'}],
  });

  print(result.body['choices'][0]['message']['content']);
  if (result.meta.isPriced) {
    print('Cost: \$${result.meta.cost} | ID: ${result.meta.requestId}');
  }
  client.close();
}

SSE Streaming

final stream = client.messagesStream({
  'model': 'claude-haiku-4-5-20251001',
  'max_tokens': 256,
  'messages': [{'role': 'user', 'content': 'Write a haiku.'}],
});

await for (final chunk in stream) {
  print(chunk.delta);
}

Error Handling

Gateway refusals map to subclasses of NRouterError:

TypeHTTP StatusDescription
NRouterGuardrailBlockedError400Blocked by configured guardrail
NRouterAuthenticationError401Invalid or revoked NROUTER_API_KEY
NRouterCreditError402Account balance or budget ceiling exhausted
NRouterRateLimitError429RPM or TPM rate limit exceeded
NRouterServiceError503Gateway or upstream provider error

isRetryable is true for NRouterRateLimitError, NRouterServiceError, and NRouterTransportError.

Next Steps

Was this page helpful?