Installation
Add swiftlyai to your pubspec.yaml:
dependencies:
swiftlyai:
git:
url: https://github.com/Swiftly-Developed/SwiftlyAIKit-Flutter-SDK.git
Pure Dart — works with Flutter, CLI, and server-side Dart. No Flutter dependency required.
Quick Start
import 'package:swiftlyai/swiftlyai.dart';
// Initialize the client
final client = SwiftlyClient(
apiKey: 'sa_your_api_key',
bundleId: 'com.yourapp',
);
// Send a chat message
final response = await client.chat(
'claude-sonnet-4-5-20250929',
[userMessage('Hello!')],
);
print(response.message);
Chat
Send a chat completion request with conversation history:
final response = await client.chat(
'claude-sonnet-4-5-20250929',
[
systemMessage('You are a helpful assistant.'),
userMessage('Explain quantum computing'),
],
);
print(response.message); // The response text
print(response.model); // Model used
print(response.usage); // Token usage info
Streaming
Stream responses in real-time using Dart Streams:
client.streamChat(
'claude-sonnet-4-5-20250929',
[userMessage('Tell me a story')],
).listen((chunk) {
stdout.write(chunk.message);
});
// Or use await for
await for (final chunk in client.streamChat(
'claude-sonnet-4-5-20250929',
[userMessage('Tell me a story')],
)) {
stdout.write(chunk.message);
}
Image Generation
Generate images from text prompts:
final image = await client.generateImage(
'A sunset over the mountains',
'dall-e-3',
);
print(image.url);
Token Counting
Count tokens before sending a request:
final count = await client.countTokens(
'claude-sonnet-4-5-20250929',
[userMessage('Hello!')],
);
print(count.totalTokens);
Available Models
List all available models:
final models = await client.getModels();
for (final provider in models.providers) {
print('${provider.name}: ${provider.models.length} models');
}
Error Handling
Handle errors with typed exceptions:
try {
final response = await client.chat(
'claude-sonnet-4-5-20250929',
[userMessage('Hello')],
);
} on SwiftlyClientError catch (e) {
print('Error: ${e.message}');
print('Code: ${e.code}');
if (e.isRetryable) {
// Retry after delay
}
if (e.isBillingError) {
// Show upgrade prompt
}
}
Quota Tracking
Monitor your usage quota:
final quota = client.currentQuota;
if (quota != null) {
print('Used: ${quota.requestsUsed} / ${quota.requestsLimit}');
print('Usage: ${quota.usagePercent}%');
if (quota.isApproachingLimit) {
// Show warning to user
}
}