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 nrouterChat 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:
| Type | HTTP Status | Description |
|---|---|---|
NRouterGuardrailBlockedError | 400 | Blocked by configured guardrail |
NRouterAuthenticationError | 401 | Invalid or revoked NROUTER_API_KEY |
NRouterCreditError | 402 | Account balance or budget ceiling exhausted |
NRouterRateLimitError | 429 | RPM or TPM rate limit exceeded |
NRouterServiceError | 503 | Gateway or upstream provider error |
isRetryable is true for NRouterRateLimitError, NRouterServiceError, and NRouterTransportError.
Next Steps
- Dart SDK Source & Demos
- Swift / iOS SDK Guide — Native Swift client
- Node.js SDK Guide — TypeScript client
- Chat Completions API — HTTP endpoint specifications
Was this page helpful?