Quickstarters
Feature Overview
How to Build a Backend for React Native?
38 min
introduction in this tutorial, you’ll learn how to build a backend for react native using back4app we will focus on cross platform compatibility and illustrate how you can integrate essential back4app features for data management, user authentication, and real time data by leveraging rest and graphql apis, you can develop your react native project to run on both ios and android platforms, ensuring a seamless experience across native components and mobile applications implementing secure user logins, scheduling tasks, and using real time applications will make your full stack developer journey easier you will also see how back4app’s environment can reduce the time needed to set up services including hosting, database, and security layers by the end, you’ll have a robust backend structure that supports your react native app and paves the way for building mobile solutions at scale after completing this guide, you’ll be ready to expand your app with advanced features, integrate third party services, or turn your project into a production ready platform let’s dive into modern mobile app development with back4app and react native! prerequisites to complete this tutorial, you will need a back4app account and a new back4app project getting started with back4app https //www back4app com/docs/get started/new parse app if you do not have an account, sign up for free then, follow the guide to get your project ready basic react native development environment you can use the react native cli quickstart https //reactnative dev/docs/environment setup or expo cli https //docs expo dev/get started/installation/ ensure you have node js installed node js (version 14 or above) installed you need node js for installing npm packages and running local development servers installing node js https //nodejs org/en/download/ familiarity with javascript and basic react native concepts react native official docs https //reactnative dev/ if you’re new to react native development, review the docs or a beginner’s tutorial first make sure you have these prerequisites in place before you begin having your back4app project created and your local react native environment configured will ensure a smooth process step 1 – creating a new project on back4app and connecting create a new project the first step in building mobile backends for your react native app is creating a new project on back4app follow these steps log in to your back4app account click the “new app” button in your back4app dashboard give your app a name (e g , “reactnative backend tutorial”) once the project is created, it will appear in your back4app dashboard you’ll use this new project to manage data and configure security for your react native app getting your application keys unlike web based react apps, react native development often requires direct http requests for data retrieval and manipulation since we are focusing on rest apis (or graphql apis ) instead of the parse sdk, you’ll still need your back4app keys to send authenticated requests retrieve your parse keys in the back4app dashboard, open your app’s app settings or security & keys section to find your application id , rest api key , and graphql endpoint (usually https //parseapi back4app com/graphql ) note your rest api key you will include it in your react native fetch or axios headers to authenticate each request this step ensures your mobile applications can securely communicate with back4app step 2 – setting up the database back4app provides a wide range of backend options for react native apps, including robust data management capabilities you can create classes, add fields, and define relationships via the dashboard whether you are building real time applications or simpler crud apps, the back4app dashboard helps you store and organize data easily creating a data model navigate to the “database” section in your back4app dashboard create a new class (e g , “todo”) and add relevant columns like title (string) and iscompleted (boolean) back4app supports various data types string , number , boolean , object , date , file , pointer , array , relation , geopoint , and polygon you can also let parse auto create fields when you send new data creating a data model with ai agent if you prefer, you can use the back4app ai agent open the ai agent from your app dashboard describe your data model in plain language (e g , “build a todo class with a title and iscompleted fields ”) let the ai agent create the schema for you this can save time during the early stages of your react native project reading and writing data using rest api for typical react native development, you can use the native fetch api or a third party library like axios to handle rest apis below is an example using curl, which you can adapt for fetch post (create a todo) curl x post \\ h "x parse application id your application id" \\ h "x parse rest api key your rest api key" \\ h "content type application/json" \\ d '{"title" "buy groceries", "iscompleted" false}' \\ https //parseapi back4app com/classes/todo get (fetch todos) curl x get \\ h "x parse application id your application id" \\ h "x parse rest api key your rest api key" \\ https //parseapi back4app com/classes/todo in your react native app, you can do the same with fetch async function gettodos() { try { const response = await fetch('https //parseapi back4app com/classes/todo', { method 'get', headers { 'x parse application id' 'your application id', 'x parse rest api key' 'your rest api key', 'content type' 'application/json', }, }); const data = await response json(); console log('fetched todos ', data results); return data results; } catch (error) { console error('error fetching todos ', error); } } reading and writing data using graphql api if you prefer graphql, back4app provides a graphql endpoint below is an example mutation to create a new record mutation { createtodo(input { fields { title "clean the house" iscompleted false } }) { todo { objectid title iscompleted } } } you can execute graphql queries using a library like apollo client or even a simple fetch call async function createtodographql() { const query = ` mutation { createtodo(input { fields { title "study react native" iscompleted false } }) { todo { objectid title } } } `; try { const response = await fetch('https //parseapi back4app com/graphql', { method 'post', headers { 'x parse application id' 'your application id', 'x parse rest api key' 'your rest api key', 'content type' 'application/json', }, body json stringify({ query }), }); const result = await response json(); console log('graphql response ', result); } catch (error) { console error('error creating todo with graphql ', error); } } working with live queries (optional) for real time data, back4app has live queries, although it typically requires the parse sdk as we are focusing on rest calls in this tutorial, you can enable live queries in your app’s server settings if you plan to use them later real time data can help you keep users updated instantly in a react native app for a simpler approach, you might poll the server with your own intervals or rely on third party tools step 3 – applying security with acls and clps brief overview back4app secures your backend with acls (access control lists) and clps (class level permissions) these let you protect data at both the object and class levels they are vital for implementing secure user permissions in production grade mobile app development step by step class level permissions (clps) go to your app’s database section, open any class, and switch to “security & permissions ” adjust read/write permissions for various user roles or public access acls you can apply per object access control by including acl fields in your rest requests for example for more details, check out the app security guidelines https //www back4app com/docs/security/parse security step 4 – writing cloud code functions why cloud code cloud code lets you run server side scripts on back4app for tasks like validations, triggers, and processing external api calls it helps you control logic that should remain hidden from the client, providing better security for your react native project example function below is an example you’d write in your main js on the server side you can call it from your react native app through rest // main js parse cloud define('generategreeting', async (request) => { const { name } = request params; if (!name) { throw 'name parameter is missing!'; } return `hello, ${name}! welcome to our react native app `; }); deployment back4app cli install the cli, configure your account key, and run b4a deploy dashboard you can also go to cloud code > functions , paste your code in main js , and click deploy calling your function (via rest) use rest apis directly from your react native app async function callcloudfunction(name) { try { const response = await fetch('https //parseapi back4app com/functions/generategreeting', { method 'post', headers { 'x parse application id' 'your app id', 'x parse rest api key' 'your rest api key', 'content type' 'application/json', }, body json stringify({ name }), }); const data = await response json(); console log('greeting ', data result); } catch (err) { console error('error calling cloud function ', err); } } this flexibility makes you a more efficient full stack developer, as you can integrate business logic without exposing sensitive details on the client step 5 – configuring authentication enable or set up user authentication back4app uses the parse user class to manage user authentication even if you are not using the parse sdk in react native, you can register, log in, or log out using direct http requests sign up a user (rest) curl x post \\ h "x parse application id your app id" \\ h "x parse rest api key your rest api key" \\ h "content type application/json" \\ d '{ "username" "alice", "password" "secretpassword", "email" "alice\@example com" }' \\ https //parseapi back4app com/users log in (rest) curl x get \\ h "x parse application id your app id" \\ h "x parse rest api key your rest api key" \\ g \\ \ data urlencode 'username=alice' \\ \ data urlencode 'password=secretpassword' \\ https //parseapi back4app com/login these requests return a session token that you can store in your react native app to manage user sessions this ensures that each request you make can be authorized, building mobile experiences that stay secure social login back4app supports social logins (google, facebook, apple) through specialized flows you’ll need to follow the social login docs https //www back4app com/docs/platform/sign in with apple to configure oauth apps, and then send appropriate tokens to back4app step 6 – handling file storage setting up file storage back4app can store files for your react native app you can attach them to objects or upload them directly since we’re using rest, below is an example of uploading a file (base64 encoded) curl x post \\ h "x parse application id your app id" \\ h "x parse rest api key your rest api key" \\ h "content type image/png" \\ \ data binary '@path to your file png' \\ https //parseapi back4app com/files/image png the response returns a url you can store in your database from your react native app, you can do this with fetch by sending the file as a blob or form data security considerations to prevent unauthorized uploads, configure the fileupload options in your parse server settings for instance, you could allow uploads only from authenticated users this ensures services including file storage remain protected step 7 – email verification and password reset overview confirming email ownership is key to implementing secure user flows back4app offers built in tools for email verification and password resets back4app dashboard configuration open your app settings enable email verification under email settings customize the templates for password reset and verification messages code/implementation a user who forgets their password can trigger a reset request curl x post \\ h "x parse application id your app id" \\ h "x parse rest api key your rest api key" \\ h "content type application/json" \\ d '{"email" "alice\@example com"}' \\ https //parseapi back4app com/requestpasswordreset back4app sends a password reset email to the user this convenience saves you from setting up separate mail servers in your react native app step 8 – scheduling tasks with cloud jobs what cloud jobs do cloud jobs help you automate recurring tasks like data cleanup or sending daily reports below is an example job in main js // main js parse cloud job('cleanupoldtodos', async (request) => { const todo = parse object extend('todo'); const query = new parse query(todo); const now = new date(); const thirty days = 30 24 60 60 1000; const cutoff = new date(now thirty days); query lessthan('createdat', cutoff); try { const oldtodos = await query find({ usemasterkey true }); await parse object destroyall(oldtodos, { usemasterkey true }); return `deleted ${oldtodos length} old todos `; } catch (err) { throw new error('error during cleanup ' + err message); } }); deploy this code, then go to server settings > background jobs to schedule it this keeps your data fresh across your ios and android platforms without manual intervention step 9 – integrating webhooks definition webhooks allow your backend to notify external services whenever an event occurs for instance, you could notify slack or a payment gateway upon creating a new todo configuration go to more > webhooks in your back4app dashboard add a new webhook pointing to a desired external endpoint set triggers to define when your react native app data changes should trigger the webhook you can also code webhooks inside cloud code triggers, allowing you to post http requests or integrate with third party apis this extends your backend capabilities to a wide range of external services step 10 – exploring the back4app admin panel where to find it the back4app admin panel is a user friendly interface for non technical individuals to manage data it’s especially useful for product owners, client representatives, or support staff who need direct access to your data model features enable admin app in your app dashboard > more > admin app create an admin user (username/password) choose a subdomain for quick, code free database access once logged in, your users or team can view, edit, or delete records without writing any code this approach supports faster data management and collaboration conclusion in this guide, you learned how to build a backend for react native applications using back4app this included creating a secure backend and implementing cross platform compatibility for your react native app setting up data management with rest and graphql apis configuring acls and clps to protect sensitive data writing cloud code for server side logic handling user authentication and email verification managing file storage with direct uploads scheduling background tasks with cloud jobs using webhooks to integrate external services exploring the back4app admin panel for easy database administration with these tools and features, your react native project can grow into a reliable and scalable full stack solution you are now equipped to handle real time data, user security, and other crucial aspects of mobile applications keep exploring the back4app documentation https //www back4app com/docs/ to refine your skills and create powerful mobile experiences across ios and android platforms next steps harden your react native app with advanced security and role based access control experiment with real time updates using live queries for real time applications (if needed) integrate external apis and services including payment gateways or social logins enhance performance through caching or optimizing cloud functions dive deeper into back4app’s official docs https //www back4app com/docs/ to unlock additional features