Skip to content
FlagSwiftDocs

Integrate with React Native

Expo and bare apps with an offline cache.

Before you start#

  • A client key (fs_client_…) from the API Keys page of your console. See API keys for how keys differ.
  • The address of your FlagSwift server, for example https://flags.example.com.
  • A flag to read. Create one in the console and turn it on in an environment.

Only client keys belong in an app people can inspect

Server and admin keys can read targeting and rollout data. Use a client key for anything that ships to a browser or a phone.

Your server#

Enter your server address and, if your key covers every environment of the project, the environment to read. The examples below fill in as you type. Nothing is sent anywhere: the values stay in this browser.

Set up#

  1. Environment

    bash
    EXPO_PUBLIC_FLAGSWIFT_KEY=fs_client_...   # from the API Keys page
  2. Install

    bash
    npm install @flagswift/react-native @react-native-async-storage/async-storage
  3. Set up

    tsx
    import AsyncStorage from "@react-native-async-storage/async-storage";
    import { createFlagClient, FlagSwiftProvider } from "@flagswift/react-native";
    
    const client = createFlagClient({
      apiKey: process.env.EXPO_PUBLIC_FLAGSWIFT_KEY!,
      baseUrl: "https://flags.example.com",
      storage: AsyncStorage, // last good flags survive restarts and offline launches
      fallbacks: { "new-onboarding": false },
    });
    
    export default function App() {
      return (
        <FlagSwiftProvider client={client} user={{ identifier: userId, device: deviceId }}>
          <Home />
        </FlagSwiftProvider>
      );
    }
  4. Use

    tsx
    import { useFlag } from "@flagswift/react-native";
    
    const Home = () => (useFlag("new-onboarding") ? <NewOnboarding /> : <Onboarding />);

Verify it works#

Before you run your app, check the key, the address and the environment with one request. A client key reads the outcome for one person.

bash
curl -s -X POST "https://flags.example.com/api/v1/evaluation/client" \
  -H "Authorization: Bearer $FLAGSWIFT_CLIENT_KEY" \
  -H "Content-Type: application/json" \
  -d '{"identifiers":["user-123"]}'

A 200 response with your flags means everything is wired up. If it fails, Troubleshooting lists the usual causes.

Next steps#