fairydust
Developer Documentation
← Back to Apps

πŸš€Getting Started

fairydust makes it simple to add AI-powered micropayments to your app. Users pay with DUST tokens for AI actions, and you handle everything in the browserβ€”no backend required.

Quick Integration

Get started in just a few lines of code:

<!-- Include fairydust SDK -->
<script src="https://fairydust.fun/sdk/simple.js"></script>
<link rel="stylesheet" href="https://fairydust.fun/sdk/fairydust.css">

<!-- Add components to your HTML -->
<div id="fairydust-account"></div>
<button id="ai-action" class="fairydust-button" data-cost="5">
  Generate with AI
</button>

<script>
// Initialize fairydust
const fairydust = new Fairydust({
    appId: 'YOUR-APP-ID'
});

// The SDK automatically enhances your buttons!
document.getElementById('ai-action').addEventListener('click', () => {
    // This runs after successful payment
    performAIAction();
});
</script>

πŸ‘€Account Component

The Account Component displays the user's connection status and DUST balance. It's the primary way users interact with their fairydust account.

Connected:

πŸ§šβ€β™€οΈ 125

Disconnected:

πŸ§šβ€β™€οΈ

Basic Usage

<!-- Automatic initialization -->
<div id="fairydust-account"></div>

<!-- Or create programmatically -->
<script>
fairydust.createAccountComponent('#account-widget', {
    onConnect: (user) => console.log('User connected:', user),
    onDisconnect: () => console.log('User disconnected'),
    onBalanceUpdate: (balance) => console.log('Balance:', balance)
});
</script>

Component Props

  • onConnectfunction(user) - Called when user successfully connects
  • onDisconnectfunction() - Called when user disconnects
  • onBalanceUpdatefunction(balance) - Called when balance changes

User Experience

Disconnected State

Shows only the fairy emoji (πŸ§šβ€β™€οΈ). Clicking opens the authentication modal.

Connected State

Shows fairy emoji and DUST balance. Clicking opens account details with options to buy DUST or disconnect.

Account Details

Modal shows user's fairyname, large balance display, and action buttons for purchasing DUST or visiting fairydust.fun.

πŸ’°Action Button

Action Buttons are the core of fairydust payments. They handle the entire payment flow and execute your AI actions after successful DUST transactions.

Automatic Enhancement

The simplest way to add payments is with automatic button enhancement:

<button class="fairydust-button" data-cost="5" onclick="generateJoke()">
  Tell me a dad joke
</button>

The SDK automatically finds buttons with the fairydust-button class and enhances them with payment functionality.

Programmatic Creation

fairydust.createButtonComponent('#generate-button', {
    dustCost: 5,
    label: 'Generate Recipe',
    onSuccess: (transaction) => {
        console.log('Payment successful:', transaction);
        generateRecipe();
    },
    onError: (error) => {
        console.error('Payment failed:', error);
    }
});

Component Props

  • dustCostnumber - Required. Amount of DUST to charge
  • labelstring - Button text (can also use children)
  • disabledboolean - Disable the button
  • classNamestring - Additional CSS classes
  • onSuccessfunction(transaction) - Called after successful payment
  • onErrorfunction(error) - Called on payment errors

Payment Flow

1. Authentication Check

If user isn't connected, shows authentication modal first.

2. Balance Check

Verifies user has sufficient DUST. Shows purchase modal if needed.

3. Confirmation

Shows payment confirmation (unless user has opted to skip confirmations).

4. Payment Processing

Processes the DUST transaction with loading animation.

5. Success Callback

Calls your onSuccess function to execute the AI action.

Skip Confirmations

Users can choose to skip payment confirmations for a smoother experience. This setting is stored per-app and remembered across sessions.

πŸ”Authentication Experience

fairydust uses a smooth two-step authentication process that works with both email and phone numbers.

Authentication Flow

Step 1: Identifier Input

User enters their email or phone number. The system automatically detects the type and validates the format.

Step 2: OTP Verification

User receives a 6-digit code via email or SMS. The form auto-submits when all digits are entered.

Modal Branding

The authentication modal shows "Powered by fairydust" and explains that new users get 25 DUST for free when they provide their phone or email.

// Programmatic authentication
fairydust.createAuthenticationComponent('#auth-modal', {
    appName: 'Your App Name',
    onSuccess: (authResponse) => {
        console.log('User authenticated:', authResponse.user);
        // Refresh account components
        fairydust.refreshAccountComponents();
    },
    onCancel: () => {
        console.log('Authentication cancelled');
    }
});

Key Features

πŸ’³Purchase Experience

Coming Soon: The DUST purchase flow is currently in development. This section outlines the planned user experience.

When users need more DUST, they'll have a seamless purchase experience integrated directly into your app.

Planned Purchase Flow

1. Insufficient Balance

When a user tries to perform an action without enough DUST, they'll see a purchase modal.

2. DUST Packages

Users will choose from preset DUST packages (e.g., 100 DUST for $1, 500 DUST for $4, 1000 DUST for $7).

3. Payment Methods

Support for credit cards, Apple Pay, Google Pay, and other popular payment methods.

4. Instant Credit

DUST is credited immediately after successful payment, allowing users to continue their action.

Integration Points

// This API is planned for the purchase experience
fairydust.createPurchaseComponent('#purchase-modal', {
    packages: [
        { dust: 100, price: 1.00 },
        { dust: 500, price: 4.00 },
        { dust: 1000, price: 7.00 }
    ],
    onSuccess: (purchase) => {
        console.log('DUST purchased:', purchase);
        // Continue with the original action
    },
    onCancel: () => {
        console.log('Purchase cancelled');
    }
});
Current Workaround: For now, users can purchase DUST by visiting their account details and clicking "Buy More Dust" which redirects to fairydust.fun.

πŸ“šAPI Reference

Fairydust Class

The main SDK class for creating and managing components.

Constructor

const fairydust = new Fairydust({
    appId: 'your-app-id',           // Required: Your registered app ID
    apiUrl: 'custom-api-url',       // Optional: Custom API endpoint
    ledgerUrl: 'custom-ledger-url', // Optional: Custom ledger endpoint
    debug: true                     // Optional: Enable debug logging
});

Methods

  • createAccountComponent(selector, props) - Create account widget
  • createButtonComponent(selector, props) - Create payment button
  • createAuthenticationComponent(selector, props) - Create auth modal
  • refreshAccountComponents() - Refresh all account components
  • getAPI() - Get the underlying API client
  • getAuthState() - Get current authentication state

Events

The SDK dispatches custom events for integration with your app:

// Listen for successful payments
document.addEventListener('fairydust:success', (event) => {
    console.log('Payment successful:', event.detail);
});

// Listen for payment errors
document.addEventListener('fairydust:error', (event) => {
    console.error('Payment failed:', event.detail);
});

CSS Classes

Key CSS classes for styling customization:

  • .fairydust-account - Account component container
  • .fairydust-button - Payment button
  • .fairydust-modal - Modal overlay
  • .fairydust-auth - Authentication form
  • .fairydust-fairy - Fairy emoji (with spin animation)

Available SDK Files

  • simple.js - Simplified version with auto-enhancement
  • index.umd.js - Universal module (browser)
  • index.esm.js - ES modules
  • index.js - CommonJS
  • fairydust.css - Default styling

🎯Next Steps

Ready to add magical AI payments to your app?

Coming Soon