erghi_sdk 1.0.0
erghi_sdk: ^1.0.0 copied to clipboard
Official Erghi SDK for Flutter - Real-time customer support chat in your iOS and Android apps
Erghi Flutter SDK #
Official Flutter SDK for the Erghi Platform โ Build AI-powered chat experiences in your iOS and Android apps.
Features #
- โ Full type-safe API with null safety
- ๐ JWT authentication with auto-refresh
- ๐ฌ Real-time messaging with WebSocket
- ๐ฑ iOS and Android support
- ๐จ Material Design widgets
- ๐ฆ Minimal dependencies
- ๐งช Fully tested
Installation #
Add to your pubspec.yaml:
dependencies:
erghi_sdk: ^1.0.0
Then run:
flutter pub get
Quick Start #
Initialize the Client #
import 'package:erghi_sdk/erghi_sdk.dart';
final client = ErghiClient(
config: ErghiConfig(
apiUrl: 'https://api.erghi.ai',
debug: true,
),
);
Authentication #
// Register
final auth = await client.auth.register(
RegisterRequest(
email: 'user@example.com',
password: 'SecurePass123!',
firstName: 'John',
lastName: 'Doe',
),
);
// Login
final auth = await client.auth.login(
LoginRequest(
email: 'user@example.com',
password: 'SecurePass123!',
),
);
// Get current user
final user = await client.auth.me();
// Logout
await client.auth.logout();
Chat Operations #
// Create conversation
final conversation = await client.chat.createConversation(
widgetId: 'your-widget-id',
metadata: {'page': '/products'},
);
// Get messages
final messages = await client.chat.getMessages(
conversation.id,
page: 1,
limit: 50,
);
// Send message
final message = await client.chat.sendMessage(
conversation.id,
SendMessageRequest(
content: 'Hello!',
type: MessageType.text,
),
);
// Mark as read
await client.chat.markAsRead(conversation.id);
Real-Time with WebSocket #
// Connect to WebSocket
await client.connectWebSocket();
// Join conversation
client.joinConversation(conversationId);
// Listen for messages
client.messageStream?.listen((message) {
print('New message: ${message.content}');
});
// Send typing indicator
client.sendTyping(conversationId);
// Leave conversation
client.leaveConversation(conversationId);
// Disconnect
client.disconnectWebSocket();
Complete Example #
import 'package:flutter/material.dart';
import 'package:erghi_sdk/erghi_sdk.dart';
class ChatScreen extends StatefulWidget {
final String conversationId;
const ChatScreen({required this.conversationId});
@override
State<ChatScreen> createState() => _ChatScreenState();
}
class _ChatScreenState extends State<ChatScreen> {
late final ErghiClient _client;
final _messageController = TextEditingController();
final List<Message> _messages = [];
@override
void initState() {
super.initState();
_initializeChat();
}
Future<void> _initializeChat() async {
_client = ErghiClient(
config: ErghiConfig(
apiUrl: 'https://api.erghi.ai',
debug: true,
),
);
// Connect WebSocket
await _client.connectWebSocket();
_client.joinConversation(widget.conversationId);
// Listen for messages
_client.messageStream?.listen((message) {
setState(() {
_messages.add(message);
});
});
// Load initial messages
final response = await _client.chat.getMessages(widget.conversationId);
setState(() {
_messages.addAll(response.items);
});
}
Future<void> _sendMessage() async {
if (_messageController.text.isEmpty) return;
final message = await _client.chat.sendMessage(
widget.conversationId,
SendMessageRequest(content: _messageController.text),
);
setState(() {
_messages.add(message);
_messageController.clear();
});
}
@override
void dispose() {
_client.dispose();
_messageController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Chat')),
body: Column(
children: [
Expanded(
child: ListView.builder(
itemCount: _messages.length,
itemBuilder: (context, index) {
final message = _messages[index];
return ListTile(
title: Text(message.content),
subtitle: Text(message.createdAt.toString()),
);
},
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
children: [
Expanded(
child: TextField(
controller: _messageController,
decoration: InputDecoration(hintText: 'Type a message'),
onChanged: (_) => _client.sendTyping(widget.conversationId),
),
),
IconButton(
icon: Icon(Icons.send),
onPressed: _sendMessage,
),
],
),
),
],
),
);
}
}
Error Handling #
try {
await client.auth.login(LoginRequest(
email: 'user@example.com',
password: 'wrong-password',
));
} on AuthenticationException catch (e) {
print('Auth error: ${e.message}');
} on NetworkException catch (e) {
print('Network error: ${e.message}');
} on ErghiException catch (e) {
print('Error: ${e.message}');
}
Platform-Specific Setup #
Android #
Add to android/app/src/main/AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
iOS #
No additional setup required!
API Documentation #
ErghiClient #
auth- Authentication operationschat- Chat operationsconnectWebSocket()- Connect to real-time updatesdisconnectWebSocket()- Disconnect WebSocketmessageStream- Stream of real-time messagessendTyping()- Send typing indicatorjoinConversation()- Join conversation roomleaveConversation()- Leave conversation roomdispose()- Clean up resources
Models #
User- User modelAuthResponse- Authentication responseConversation- Conversation modelMessage- Message modelWidget- Widget modelPaginatedResponse<T>- Paginated data
Exceptions #
ErghiException- Base exceptionAuthenticationException- Auth errorsNetworkException- Network errorsValidationException- Validation errorsNotFoundException- Resource not foundWebSocketException- WebSocket errors
Development #
# Get dependencies
flutter pub get
# Run code generation
flutter pub run build_runner build
# Run tests
flutter test
# Analyze code
flutter analyze
Contributing #
Contributions are welcome! Please read our contributing guide.
License #
MIT License - see LICENSE file
Support #
- ๐ง Email: support@erghi.ai
- ๐ฌ Discord: Join our community
- ๐ Issues: GitHub Issues
Made with โค๏ธ by the Erghi team