This guide will walk you through the process of integrating the Capsule Modal into your web application, providing a seamless way to manage user authentication and wallets.

The Capsule Web SDK is React-based, however there are a variety of adaptions necessary for different frameworks such as Next.JS and Vite, as well as different build systems (CRACO, Webpack, etc).

Please reference the framework-specific troubleshooting sections first when encountering build errors, and don’t hesitate to reach out for help.

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.

Installing Dependencies

In this section, you’ll install the Capsule React SDK, which is the core dependency needed to integrate Capsule into your web application.

First, install the Capsule React SDK using your preferred package manager:

Setting Up the Capsule SDK

Now that you’ve installed the necessary dependencies, let’s set up the Capsule SDK. This will involve creating a client instance, setting up polyfills, and integrating the Capsule Modal into your application.

Creating a Capsule Client Instance

First, create a Capsule client instance. This instance will be used to interact with the Capsule SDK and manage the authentication flow.

import Capsule, { Environment } from "@usecapsule/react-sdk";
const capsule = new Capsule(Environment.BETA, process.env.REACT_APP_CAPSULE_API_KEY);

// Verify the instance is created successfully
console.log("Capsule instance created:", capsule);

This step initializes the Capsule SDK with your API key and the chosen environment. If successful, you should see the Capsule instance logged in your console.

Capsule offers two hosted environments: Environment.BETA (alias Environment.DEVELOPMENT) for testing, and Environment.PROD (alias Environment.PRODUCTION) for live use. Select the environment that matches your current development phase.

Setting Up Polyfills

Depending on your target environment, you may need to set up polyfills for certain features that Capsule relies on. This step ensures that all required functionalities are supported in your application.

Common polyfills include:crypto,buffer,stream,process

The exact method for including these polyfills will depend on your build tool and framework. Here’s a general example using webpack:

// webpack.config.js
module.exports = {
  // ...other config
  resolve: {
    fallback: {
      crypto: require.resolve("crypto-browserify"),
      buffer: require.resolve("buffer/"),
      stream: require.resolve("stream-browserify"),
    },
  },
  plugins: [
    new webpack.ProvidePlugin({
      process: "process/browser",
      Buffer: ["buffer", "Buffer"],
    }),
  ],
};

Make sure to install the necessary packages (e.g., crypto-browserify, buffer, etc.) before using them in your config.

If you’re using Vite, consider using the Vite Plugin Node Polyfills to simplify the polyfilling process. If you’re using Create React App (CRA), you can use CRACO to modify the webpack configuration as shown above.

The specific polyfills and setup may vary based on your project’s needs and the framework you’re using. Consult your framework’s documentation for best practices on polyfilling or consult our troubleshooting guide for more information.

Integrating the Capsule Modal

Now, integrate the Capsule Modal into your React component by importing the CapsuleModal component from the Capsule SDK, adding a button to trigger the modal, and handling the modal’s state.

Beta Testing Credentials In the BETA Environment, you can use any email ending in @test.usecapsule.com (like dev@test.usecapsule.com) or US phone numbers (+1) in the format (area code)-555-xxxx (like (425)-555-1234). Any OTP code will work for verification with these test credentials. These credentials are for beta testing only. You can delete test users anytime in the beta developer console to free up user slots.

import React, { useState } from "react";
import { CapsuleModal } from "@usecapsule/react-sdk";
import "@usecapsule/react-sdk/styles.css";

function App() {
  const [isOpen, setIsOpen] = useState(false);
  return (
    <div>
      <button onClick={() => setIsOpen(true)}>Sign in with Capsule</button>
      <CapsuleModal
        capsule={capsule}
        isOpen={isOpen}
        onClose={() => setIsOpen(false)}
      />
    </div>
  );
}
export default App;

This creates a button that, when clicked, opens the Capsule Modal for user authentication. The Capsule Modal handles all aspects of user authentication and wallet management.

Make sure to import the Capsule CSS file (@usecapsule/react-sdk/styles.css) to apply the default styles to the modal.

If you’re using Next.js or another server-side rendering framework, you’ll need to use dynamic imports for the CapsuleModal component to avoid SSR-related issues. An example of dynamic import in Next.js:

import dynamic from "next/dynamic";

const CapsuleModal = dynamic(
  () => import("@usecapsule/react-sdk")
  .then((mod) => mod.CapsuleModal), { ssr: false }
  );

This ensures that the CapsuleModal is only loaded on the client-side. You may need to adjust this approach based on your specific SSR framework.

Consult your framework’s documentation or community resources for best practices on integrating client-side components in SSR applications.

Customizing the Capsule Modal

To provide the best experience for your users, you can customize the appearance and behavior of the Capsule Modal to match your application’s branding.

When rendering the CapsuleModal component, you can pass props to customize its appearance and behavior. This allows you to adjust the look and feel of the modal to align with your application’s branding.

<CapsuleModal
  capsule={capsule}
  isOpen={isOpen}
  onClose={() => setIsOpen(false)}
  appName="Your App Name"
  logo="https://yourapp.com/logo.png"
  theme={{
    backgroundColor: "#ffffff",
    foregroundColor: "#000000",
  }}
  oAuthMethods={["GOOGLE", "TWITTER", "DISCORD"]}
/>

For a full list of available CapsuleModalProps, refer to the customization guide:

Examples

For practical implementation of the Capsule SDK in React applications, explore our example repository. This repository contains real-world examples to help you get started.

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 Modal, here are some common problems and their solutions:

For a more comprehensive list of solutions, visit our troubleshooting guide:

Integration Support

If you’re experiencing issues that aren’t resolved by our troubleshooting resources, please contact our team for assistance. To help us resolve your issue quickly, please include the following information in your 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.