reachu_flutter_sdk 1.0.0
reachu_flutter_sdk: ^1.0.0 copied to clipboard
A Dart/Flutter SDK to manage shopping carts and payments over GraphQL.
reachu_flutter_sdk #
A Dart/Flutter SDK to manage shopping carts, checkouts, discounts, markets, channels and payments using GraphQL.
It is designed to be a simple and strongly typed client for ecommerce integrations.
๐ฆ Installation #
Add this to your pubspec.yaml
:
dependencies:
reachu_flutter_sdk: ^0.0.1
Then install:
dart pub get
๐ Getting Started #
Import the SDK:
import 'package:reachu_flutter_sdk/reachu_flutter_sdk.dart';
Initialize the client:
final sdk = SdkClient(
baseUrl: 'https://graph-ql-dev.reachu.io',
apiKey: 'API_KEY', // โ ๏ธ Use "Bearer <TOKEN>" if your API requires it
);
๐ Use with Singleton + .env (recommended) #
Initialize the SDK once and reuse it anywhere in your app.
Optional deps:
- Add
.env
support to keep secrets out of source control.
pubspec.yaml
dependencies: flutter_dotenv: ^5.1.0 flutter: assets: - .env
- Create a
.env
file in your project root:
GRAPHQL_ENDPOINT=https://graph-ql-dev.reachu.io
API_KEY=Bearer <YOUR_TOKEN>
- Create a singleton (e.g.
lib/services/sdk.dart
):
import 'package:reachu_flutter_sdk/reachu_flutter_sdk.dart';
class SdkService {
static final SdkService _instance = SdkService._internal();
late final SdkClient sdk;
factory SdkService() => _instance;
SdkService._internal();
void init({required String baseUrl, required String apiKey}) {
sdk = SdkClient(baseUrl: baseUrl, apiKey: apiKey);
}
}
- Initialize it in
main.dart
:
import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:uuid/uuid.dart';
import 'services/sdk.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await dotenv.load(fileName: ".env");
SdkService().init(
baseUrl: dotenv.env['GRAPHQL_ENDPOINT']!,
apiKey: dotenv.env['API_KEY']!,
);
final uuid = const Uuid().v4();
final createdCart = await SdkService().sdk.cart.create(
customer_session_id: uuid,
currency: 'USD',
);
runApp(MyApp(initialCartId: createdCart.cartId));
}
class MyApp extends StatelessWidget {
final String initialCartId;
const MyApp({super.key, required this.initialCartId});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Online Store',
home: Scaffold(body: Center(child: Text('Cart: $initialCartId'))),
);
}
}
- Use it anywhere:
import 'package:your_app/services/sdk.dart';
Future<void> addItemToCart(String cartId) async {
final updated = await SdkService().sdk.cart.addItem(
cart_id: cartId,
line_items: {'product_id': 123, 'variant_id': 456, 'quantity': 1},
);
print(updated.toJson());
}
๐ Usage Examples #
Create and Get a Cart #
final cart = await sdk.cart.create(
customer_session_id: 'session-123',
currency: 'USD',
);
print(cart.toJson());
final fetched = await sdk.cart.getById(cart_id: cart.cartId);
print(fetched.toJson());
Add an Item #
final updatedCart = await sdk.cart.addItem(
cart_id: cart.cartId,
line_items: {
'product_id': 123,
'quantity': 2,
'variant_id': 456,
},
);
print(updatedCart.toJson());
Checkout #
final checkout = await sdk.checkout.create(cartId: cart.cartId);
print(checkout.toJson());
Payments #
final intent = await sdk.payment.createPaymentIntentStripe(
checkoutId: checkout.id,
);
print(intent['client_secret']);
Discounts #
final discounts = await sdk.discount.get();
print(discounts.map((d) => d.toJson()).toList());
Markets #
final markets = await sdk.market.getAvailable();
print(markets.map((m) => m.toJson()).toList());
Channels #
final categories = await sdk.channel.category.get();
print(categories.map((c) => c.toJson()).toList());
final products = await sdk.channel.product.get(
currency: 'USD',
useCache: true,
);
print(products.map((p) => p.toJson()).toList());
๐ Modules #
- Cart โ Create, update, delete carts and line items.
- Checkout โ Manage checkout sessions.
- Payment โ Integrations with Stripe, Klarna, Vipps.
- Discount โ Create, verify and manage discounts.
- Market โ Query available markets and currencies.
- Channel โ Products, categories, and purchase conditions.
๐งช Example Project #
A runnable example is included in the example/
folder.
Run with:
dart run example/main.dart
๐ Changelog #
See CHANGELOG.md for version history.
๐ License #
This project is licensed under the MIT License - see the LICENSE file for details.