Anthropic from Flutter / Dart
SDK examples · All languages and providers · API reference
There is no official Dart SDK from Anthropic; package:http is all you need. A community SDK works too if it accepts a custom base URL and sends the token as Authorization: Bearer. Your Anthropic key stays on the server; the app holds only a token that expires in one hour.
1. Set up the service
In the dashboard, create an app, add a service and store your Anthropic key in it:
claude → https://api.anthropic.com
Set the base URL without /v1: the Anthropic API path is /v1/messages, so a service that ends in /v1 would produce /v1/v1/messages.
2. Get a token for the user
The app asks MyAppToken for a token with its app ID and your identifier for the user. The token lasts one hour; rate limits and analytics are tied to that user.
import 'dart:convert'; import 'package:http/http.dart' as http; const api = 'https://api.myapptoken.com/api/v1'; const appId = '<app-id>'; Future<String> sessionToken(String userId) async { final res = await http.post( Uri.parse('$api/auth/simple'), headers: {'Content-Type': 'application/json'}, body: jsonEncode({'app_id': appId, 'user_id': userId}), ); if (res.statusCode != 200) throw Exception('auth failed: ${res.statusCode}'); return (jsonDecode(res.body) as Map<String, dynamic>)['access_token'] as String; }
Renew it before it expires with POST /auth/refresh (send the current, still-valid token as Authorization: Bearer); if that fails, request a new one. With Firebase App Check enabled, also send the App Check header on proxy calls.
3. Call Anthropic
const proxy = 'https://api.myapptoken.com/api/v1/proxy/<app-id>/claude'; Future<Map<String, dynamic>> message(String jwt, String text) async { final res = await http.post( Uri.parse('$proxy/v1/messages'), headers: {'Authorization': 'Bearer $jwt', 'Content-Type': 'application/json'}, body: jsonEncode({ 'model': 'claude-sonnet-5-5', 'max_tokens': 1024, 'messages': [{'role': 'user', 'content': text}], }), ); return jsonDecode(res.body) as Map<String, dynamic>; } final jwt = await sessionToken('user-123'); final reply = await message(jwt, 'Hello'); print(reply['content'][0]['text']);
What changed
| Direct to the provider | Through MyAppToken | |
|---|---|---|
| Base URL | https://api.anthropic.com | https://api.myapptoken.com/api/v1/proxy/<app-id>/claude |
| Credential | provider key (sk-ant-…) | the user's token from /auth/simple |
More: Anthropic guide · rate limits · async jobs.
Start free — no credit card; the Free plan covers 10,000 requests a month.