Privy Flutter SDK

The Privy Flutter SDK is a Flutter plugin that connects your application to native Privy SDKs for iOS and Android. It enables authentication, non-custodial embedded wallets, and user management by leveraging Privy's platform-specific capabilities.

Features

Authentication

  • Login with Phone
  • Login with Email
  • Login with Custom Auth

Embedded Wallets (Ethereum & Solana)

  • Wallet Creation
  • Automatic Recovery
  • Signing Messages & Transactions

Ethereum-Only Features

  • Broadcasting Transactions
  • Multiple Embedded Wallets

Getting Started

Requirements

  • Flutter: 3.24.0+
  • Dart: 3.0.0+
  • Android: API 27+ (8.1 Oreo or newer)
  • iOS: 16+

Installation

Add the latest Privy SDK dependency to your pubspec.yaml:

dependencies:
  flutter:
    sdk: flutter
  privy_flutter: ^0.0.1  # Replace with the latest version

Run:

flutter pub get

Configuration

Registering Your App

You must configure your App ID and Client ID in the Privy Developer Dashboard.

  1. iOS: Add your app's Bundle Identifier under Settings → Clients.
  2. Android: Add your Application ID from build.gradle.
  3. Register Allowed URL Schemes for OAuth authentication in Info.plist (iOS) and AndroidManifest.xml (Android).

Initialization

Import and configure Privy in your Flutter app:

import 'package:privy_flutter/privy_flutter.dart';

final privyConfig = PrivyConfig(
  appId: "YOUR_APP_ID",
  appClientId: "YOUR_CLIENT_ID",
);

final privy = Privy(config: privyConfig);

Await SDK Readiness

When the Privy SDK is initialized, the user's authentication state will be set to NotReady until Privy finishes initialization. During this time, we suggest you show a loading state to your user. For you convenience, we've added a suspending awaitReady() function. Here's an example with some pseudocode:

await privy.awaitReady();

Authentication

Login with Email

Authenticate users via email-based OTP verification.

Step 1: Send OTP to User’s Email

final result = await privy.email.sendCode("user@example.com");
  • Success(): OTP was sent successfully.
  • Failure(PrivyException): Error sending OTP.

Step 2: Verify OTP and Login

final loginResult = await privy.email.loginWithCode(code: "123456", email: "user@example.com");
  • Success(PrivyUser): User authenticated successfully.
  • Failure(PrivyException): Invalid OTP or authentication failed.

Login with Phone

Authenticate users via SMS-based OTP verification.

Step 1: Send OTP to User’s Phone Number

final result = await privy.sms.sendCode("+14155552671");
  • Success(): OTP was sent successfully.
  • Failure(PrivyException): Error sending OTP.

Step 2: Verify OTP and Login

final loginResult = await privy.sms.loginWithCode(code: "123456", phoneNumber: "+14155552671");
  • Success(PrivyUser): User authenticated successfully.
  • Failure(PrivyException): Invalid OTP or authentication failed.

Login with Custom Auth

Authenticate users using a third-party authentication provider.

final loginResult = await privy.customAuth.loginWithCustomAccessToken();
  • Success(PrivyUser): User authenticated successfully.
  • Failure(PrivyException): Invalid or expired token.

Get Access Token

Retrieve the user's authentication token for API requests.

final getAccessTokenResult = await privy.user?.getAccessToken();
  • Success(String): Token retrieved successfully.
  • Failure(PrivyException): User not authenticated or error occurred.

Embedded Wallets

Create an Ethereum Wallet

Creates a non-custodial Ethereum embedded wallet for the user.

final walletResult = await privy.user?.createEthereumWallet();
  • Success(EmbeddedEthereumWallet): Wallet created successfully.
  • Failure(PrivyException): User not authenticated, network error, or wallet limit exceeded.

Create a Solana Wallet

Creates a non-custodial Solana embedded wallet for the user.

final walletResult = await privy.user?.createSolanaWallet();
  • Success(EmbeddedSolanaWallet): Wallet created successfully.
  • Failure(PrivyException): User not authenticated or network error.

Sign a Message (Solana)

Sign a message using the user’s Solana embedded wallet.

final signature = await privy.user?.embeddedSolanaWallets.first.provider.signMessage("message");
  • Success(String): Signature generated successfully.
  • Failure(PrivyException): User not authenticated or wallet not found.

Logout

Logs the user out and clears the persisted session.

await privy.logout();
  • Success(): User logged out successfully.
  • Failure(PrivyException): Error during logout.

Error Handling

All SDK responses use Result<T>. Handle success or failure using either .fold() or a switch statement.

Using .fold()

The .fold() method allows you to execute separate callbacks for success and failure.

result.fold(
  onSuccess: (user) => print("Success: ${user.id}"),
  onFailure: (error) => print("Error: ${error.message}"),
);

Using a switch Statement

Alternatively, you can use a switch statement to handle success and failure.

switch (result) {
  case Success(:final user): // Extracts `user` from Success<T>
    print("Success: ${user.id}");
    break;
  
  case Failure(:final error): // Extracts `error` from Failure<T>
    print("Error: ${error.message}");
    break;
}

License

This project is licensed under the MIT License.


For more details, visit the Privy Developer Dashboard.

Libraries

privy_flutter