This guide explains how to configure the mobile app to connect to your backend API, especially when you've moved the mobile folder to a new location or want to connect to a different backend server.
The mobile app needs to know where your backend API is running. By default, it's configured for local development, but you'll need to update this when:
- Moving the mobile folder to a standalone project
- Deploying to a remote server
- Testing on a physical device
- Connecting to a different backend environment
The API endpoint is configured in: src/services/api.ts
Open src/services/api.ts and find this line near the top:
const API_URL = 'http://10.0.2.2:3000';Replace the API_URL value based on your scenario:
For Android Emulator:
const API_URL = 'http://10.0.2.2:3000';
10.0.2.2is the special IP that Android emulator uses to access the host machine's localhost
For iOS Simulator:
const API_URL = 'http://localhost:3000';For Physical Device on Same Network:
const API_URL = 'http://192.168.1.100:3000';Replace
192.168.1.100with your computer's actual IP address on your local network
Production/Staging Server:
const API_URL = 'https://api.yourdomain.com';Use HTTPS in production for security
Development Server:
const API_URL = 'http://dev.yourdomain.com:3000';For better environment management, you can use environment variables:
const API_URL = __DEV__
? 'http://10.0.2.2:3000' // Development
: 'https://api.yourdomain.com'; // ProductionIf testing on a physical device connected to the same WiFi:
Windows:
ipconfigLook for "IPv4 Address" under your active network adapter (usually starts with 192.168.x.x or 10.0.x.x)
Mac/Linux:
ifconfig | grep "inet "or
ip addr showQuick Method (Any OS):
- Windows: Settings → Network & Internet → WiFi → Properties
- Mac: System Preferences → Network → Select WiFi → IP Address
Before testing the mobile app, ensure your backend:
-
Is running - Start your backend server
-
Allows connections - Check firewall settings
-
Listens on the correct interface:
- For physical devices: Backend must listen on
0.0.0.0(all interfaces), not justlocalhost - Check your backend's main file (e.g.,
src/main.tsin NestJS):await app.listen(3000, '0.0.0.0');
- For physical devices: Backend must listen on
-
Has CORS enabled - Your backend must allow requests from the mobile app
After updating the API_URL:
-
Restart the Metro bundler:
npm start -- --reset-cache
-
Rebuild the app:
# Android npm run android # iOS npm run ios
-
Test login/registration to verify the connection works
You moved the mobile folder to ~/Projects/my-mobile-app/:
// src/services/api.ts
const API_URL = 'http://10.0.2.2:3000'; // Android
// OR
const API_URL = 'http://localhost:3000'; // iOSBackend must be running on port 3000 on your computer.
Your backend is hosted at api.myapp.com:
// src/services/api.ts
const API_URL = 'https://api.myapp.com';No port needed if using standard HTTPS (443).
Your computer's local IP is 192.168.1.50, backend runs on port 3000:
// src/services/api.ts
const API_URL = 'http://192.168.1.50:3000';Create a config file src/config/api.config.ts:
const ENV = {
development: {
apiUrl: __DEV__ ? 'http://10.0.2.2:3000' : 'http://localhost:3000',
},
staging: {
apiUrl: 'https://staging-api.myapp.com',
},
production: {
apiUrl: 'https://api.myapp.com',
},
};
export const API_CONFIG = ENV.development; // Change based on environmentThen in src/services/api.ts:
import { API_CONFIG } from '../config/api.config';
const API_URL = API_CONFIG.apiUrl;Cause: Mobile app can't reach the backend
Solutions:
- Verify backend is running:
curl http://localhost:3000or visit in browser - Check firewall isn't blocking the port
- For physical devices: Ensure mobile and computer are on same WiFi
- For Android emulator: Use
10.0.2.2instead oflocalhost - Check the API_URL is correct in
api.ts
Cause: Backend not allowing requests from mobile app
Solution: Update backend CORS configuration:
// In your backend (e.g., NestJS main.ts)
app.enableCors({
origin: '*', // Or specify your mobile app's origin
credentials: true,
});Cause: Physical device can't access localhost
Solution:
- Find your computer's IP address (see Step 3 above)
- Update API_URL to use your IP:
http://192.168.1.X:3000 - Ensure backend listens on
0.0.0.0, not just127.0.0.1
Cause: Invalid URL or DNS issue
Solutions:
- Check for typos in API_URL
- Verify domain exists if using remote server
- Try using IP address instead of domain name
- Check internet connection on device
- Use HTTP for local development
- Use
10.0.2.2orlocalhostfor emulators/simulators
- Always use HTTPS for production APIs
- Never hardcode sensitive credentials in the app
- Use environment variables for API keys
- Implement certificate pinning for additional security
- Use secure token storage (already implemented with AsyncStorage)
- Default backend port in this project:
3000 - If your backend uses a different port, update accordingly:
http://your-ip:YOUR_PORT
The mobile app uses these main endpoint prefixes:
/auth/*- Authentication (login, register, verify email, etc.)/events/*- Event management/users/*- User settings
All endpoints are relative to the API_URL base.
To quickly switch between backends during development, you can create a simple switcher:
// src/services/api.ts
const BACKENDS = {
local: 'http://10.0.2.2:3000',
staging: 'https://staging-api.myapp.com',
production: 'https://api.myapp.com',
};
const API_URL = BACKENDS.local; // Change this to switch backendsIf you're still having connection issues:
- Check backend logs for incoming requests
- Use a tool like Postman to verify backend endpoints work
- Enable network debugging in React Native Debugger
- Check Metro bundler terminal for error messages
- Verify your backend's CORS and firewall settings
When moving the mobile folder or connecting to a new backend:
- Update
API_URLinsrc/services/api.ts - Ensure backend is running and accessible
- Configure backend to listen on
0.0.0.0(for physical devices) - Enable CORS on backend
- Use correct URL format (http/https, IP/domain, port)
- Restart Metro bundler with cache clear
- Rebuild the mobile app
- Test login/registration functionality