When I first started building mobile apps, I spent more time configuring Node.js servers and managing MongoDB clusters than actually building the UI. If you’re looking for a way to bypass that grind, this appwrite tutorial for react native is for you. Appwrite is an open-source Backend-as-a-Service (BaaS) that gives you everything—auth, databases, functions, and storage—out of the box.
In my experience, choosing between different platforms can be overwhelming. While some developers prefer Supabase vs Firebase for mobile apps, Appwrite stands out because it can be self-hosted via Docker, giving you total control over your data sovereignty. Let’s dive into how to get it running in your React Native project.
Prerequisites
- Node.js (v18+) installed on your machine.
- A React Native project (Expo or CLI). I’ll be using Expo for this guide as it’s the fastest way to prototype.
- An Appwrite account (Cloud version or a self-hosted instance).
- Basic knowledge of JavaScript/TypeScript and React hooks.
Step 1: Setting Up Your Appwrite Project
Before writing code, we need a backend environment. Log into your Appwrite console and create a new project. Once the project is created, you’ll need to add a ‘Platform’.
Click on Add Platform → Android (and iOS if applicable). You’ll need to provide a Package Name (e.g., com.yourname.myapp). This is crucial because Appwrite uses this to validate requests coming from your mobile app.
Step 2: Installing the Appwrite SDK
Now, let’s bring Appwrite into our React Native environment. Run the following command in your terminal:
npm install react-native-appwrite
I recommend creating a dedicated helper file to initialize the Appwrite client. This prevents you from re-initializing the client in every component, which can lead to memory leaks and redundant connections.
// src/lib/appwrite.js
import { Client, Account, Databases, Storage } from 'react-native-appwrite';
const client = new Client();
client
.setEndpoint('https://cloud.appwrite.io/v1') // Replace with your endpoint
.setProject('your_project_id'); // Replace with your project ID
export const account = new Account(client);
export const databases = new Databases(client);
export const storage = new Storage(client);
export { client };
Step 3: Implementing User Authentication
The most common use case in any appwrite tutorial for react native is handling users. Appwrite makes this incredibly simple. Here is how I typically implement a basic sign-up flow:
import { account } from './src/lib/appwrite';
import { ID } from 'react-native-appwrite';
async function handleSignUp(email, password, name) {
try {
const response = await account.create(
ID.unique(),
email,
password,
name
);
console.log('User created successfully:', response);
} catch (error) {
console.error('Signup error:', error.message);
}
}
For those wondering about the best backend as a service for flutter or other frameworks, the SDK pattern remains remarkably similar across Appwrite’s various platform integrations.
Step 4: Working with the Database
Appwrite uses a document-based database. To store data, you first create a Database and then a Collection within that database in the Appwrite Console. Don’t forget to set your Permissions to ‘Any’ or ‘Users’ so your app can actually read and write data.
Here is how to create a new document in a collection:
import { databases } from './src/lib/appwrite';
import { ID } from 'react-native-appwrite';
async function addTask(taskText) {
try {
const response = await databases.createDocument(
'database_id',
'collection_id',
ID.unique(),
{ task: taskText, completed: false }
);
return response;
} catch (error) {
console.error('Database error:', error.message);
}
}
Pro Tips for Production
- Environment Variables: Never hardcode your Project ID in your files. Use
react-native-dotenvor Expo’sextraconstants to keep your keys secure. - Session Persistence: Appwrite handles session cookies automatically, but for React Native, I suggest wrapping your app in an
AuthProvidercontext to manage the globaluserstate. - Index Optimization: If you’re querying large datasets, create indexes in the Appwrite console first, otherwise, your queries will fail with a 400 error.
Troubleshooting Common Issues
If you encounter a Network Request Failed error, check these three things:
- Android Emulator: If self-hosting, use
http://10.0.2.2instead oflocalhost. - Platform ID: Ensure the Bundle ID/Package Name in the Appwrite console exactly matches your
app.jsonorAndroidManifest.xml. - CORS/Permissions: Double-check that the collection permissions allow the authenticated user to perform the action.
What’s Next?
Now that you have the basics of this appwrite tutorial for react native down, I suggest exploring Appwrite Functions. They allow you to write server-side logic (in Node, Python, or Dart) that triggers on specific events, such as sending a welcome email when a user signs up.