The Capsule SDK for Flutter allows you to easily integrate secure and scalable wallet functionalities into your mobile applications. This guide covers the installation, setup, and usage of the Capsule SDK, including handling authentication flows.

Prerequisites

To use Capsule, you need an API key. This key authenticates your requests to Capsule services and is essential for integration.

Don’t have an API key yet? Request access to the Developer Portal to create API keys, manage billing, teams, and more.

Dependency Installation

To install the Capsule SDK and required dependencies, use the following command:

flutter pub add capsule

Project Setup

To set up associated domains for passkey functionality in your Flutter project, you need to configure both iOS and Android platforms:

To enable passkeys on iOS, you need to set up associated domains in your Xcode project:

  1. Open your Flutter project’s iOS folder in Xcode
  2. Select your target and go to “Signing & Capabilities”
  3. Click ”+ Capability” and add “Associated Domains”
  4. Add the following domains:
    • webcredentials:app.beta.usecapsule.com
    • webcredentials:app.usecapsule.com

For more details, see the Apple Developer documentation.

Important: You must register your teamId + bundleIdentifier with the Capsule team to set up associated domains. For example, if your Team ID is A1B2C3D4E5 and Bundle Identifier is com.yourdomain.yourapp, provide A1B2C3D4E5.com.yourdomain.yourapp to Capsule. This is required by Apple for passkey security. Allow up to 24 hours for domain propagation.

Using the Capsule SDK

The Capsule SDK provides two main authentication flows: creating a new user and logging in an existing user. Both flows utilize passkeys for secure and seamless authentication. Follow the steps below to implement these flows in your Flutter application.

Capsule Initialization

First, initialize the Capsule SDK within your application. This step is crucial for setting up the necessary environment.

import 'package:capsule/capsule.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';

late Capsule _capsule;

void initCapsule() {
  _capsule = Capsule(environment: Environment.beta, apiKey: dotenv.env['CAPSULE_API_KEY']!);
  _capsule.init();
}

Ensure that you have properly set up your environment variables and initialized Capsule before proceeding with user authentication.

Create New User

This flow guides you through the process of registering a new user, verifying their email, and setting up their wallet. The steps below should be executed sequentially within the same function or flow.

1

Check User Existence

First, check if a user with the given email already exists in the Capsule system. This will determine whether to proceed with user creation or an alternative flow.

bool userExists;
try {
  userExists = await _capsule.checkIfUserExists(email);
} catch (e) {
  print("Error checking user existence: \$e");
  return;
}

if (userExists) {
  print("User already exists. Please use passkey login.");
  return;
}
2

Create New User

If the user does not exist, proceed to create a new account. This step will automatically trigger a verification email to the provided email address.

try {
  await _capsule.createUser(email);
  print("User created. Verification email sent.");
} catch (e) {
  print("Error creating user: \$e");
  return;
}

The verification email contains a code that the user must enter to verify their email address.

3

Verify Email and Generate Passkey

Once the user receives the verification code, verify their email. After successful verification, use the biometrics ID to generate a passkey for the user. This passkey will be used for future authentications.

String? biometricsId;
try {
  biometricsId = await _capsule.verifyEmail(verificationCode);
  print("Email verified successfully");
} catch (e) {
  print("Error verifying email: \$e");
  return;
}

if (biometricsId == null) {
  print("Verification failed. Please try again.");
  return;
}

try {
  await _capsule.generatePasskey(email, biometricsId);
  print("Passkey generated successfully");
} catch (e) {
  print("Error generating passkey: \$e");
  return;
}
4

Create User Wallet

Finally, create a wallet for the user. This step will provide a recovery secret that must be securely stored.

try {
  final walletResult = await _capsule.createWallet(skipDistribute: false);
  final wallet = walletResult.wallet;
  print("Wallet created: \${wallet.address}");
  // Store or display the wallet.recoverySecret securely
} catch (e) {
  print("Error creating wallet: \$e");
  return;
}

It’s crucial to securely store or display the wallet.recoverySecret. This secret is necessary for account recovery if the user loses access to their device.

Login Existing User

This flow demonstrates how to authenticate an existing user using their email and passkey. The steps below should be executed sequentially to ensure proper authentication.

1

Initiate User Login

Start the login process by initiating the user login, which prompts the user to authenticate using their existing passkey.

Wallet wallet;
try {
  wallet = await _capsule.login();
  print("User logged in successfully");
} catch (e) {
  print("Error during login: \$e");
  return;
}

// Handle wallet information after successful login
final address = wallet.address;
print("Wallet address: \$address");

Examples

To help you get started with the Capsule Flutter SDK, we’ve prepared a comprehensive example:

This example repository demonstrates how to handle user authentication, wallet creation, and signing messages using the Capsule SDK in a Flutter application.

Next Steps

After integrating Capsule, you can explore other features and integrations to enhance your Capsule experience. Here are some resources to help you get started:

Ecosystems

Learn how to use Capsule with popular Web3 clients and wallet connectors. We’ll cover integration with key libraries for EVM, Solana, and Cosmos ecosystems.

If you’re ready to go live with your Capsule integration, make sure to review our go-live checklist:

Troubleshooting

If you encounter issues during the integration or usage of the Capsule SDK in your Flutter application, here are some common problems and their solutions:

For more detailed troubleshooting and solutions specific to Flutter, please refer to our comprehensive troubleshooting guide:

Support

If you’re experiencing issues that aren’t resolved by our troubleshooting resources, please contact our support team for assistance. To help us serve you better, include the following information in your support request:

  1. 1

    A detailed description of the problem you’re encountering.

  2. 2

    Any relevant error messages or logs.

  3. 3

    Steps to reproduce the issue.

  4. 4

    Details about your system or environment (e.g., device, operating system, software version).

Providing this information will enable our team to address your concerns more efficiently.