|
| 1 | +--- |
| 2 | +title: Connecting to a Local API from a Physical Device |
| 3 | +description: A guide to configuring the development environment to test the mobile app on a physical device with a local API server. |
| 4 | +--- |
| 5 | +import { Steps, Aside } from '@astrojs/starlight/components'; |
| 6 | + |
| 7 | +This guide explains how to connect the mobile client running on a physical device to an API server running on your local machine. |
| 8 | + |
| 9 | +### The `localhost` Challenge |
| 10 | + |
| 11 | +When the API server runs locally, it is accessible at `http://localhost:8080` on your computer. However, a physical mobile device connected to the same network cannot access your computer using the `localhost` address, as `localhost` refers to the device itself. |
| 12 | + |
| 13 | +To resolve this, your local API server must be made accessible over the network. A tunneling service like **ngrok** can accomplish this by creating a secure public URL that forwards traffic to your local server. |
| 14 | + |
| 15 | +<Aside> |
| 16 | +The use of tunneling services is intended for development and testing purposes only and is not a substitute for a properly deployed production environment. |
| 17 | +</Aside> |
| 18 | + |
| 19 | +### Configuration Steps |
| 20 | + |
| 21 | +<Steps> |
| 22 | +1. **Start Your Local API Server** |
| 23 | + |
| 24 | + Ensure your API server is running locally on the designated port. |
| 25 | + - See the [API Server Local Setup Guide](/docs/api-server/local-setup/) for instructions. |
| 26 | + |
| 27 | +2. **Install and Run ngrok** |
| 28 | + |
| 29 | + You will need to install `ngrok` and use it to expose your local server's port (e.g., `8080`). For detailed instructions on installation and usage, please refer to the official documentation. |
| 30 | + |
| 31 | + - **Official Guide:** [ngrok Documentation](https://ngrok.com/docs) |
| 32 | + |
| 33 | + Once installed, run the following command to expose your local server: |
| 34 | + ```bash |
| 35 | + ngrok http 8080 |
| 36 | + ``` |
| 37 | + ngrok will provide a public forwarding URL (e.g., `https://....ngrok-free.app`). |
| 38 | + |
| 39 | +3. **Set the Development Environment** |
| 40 | + |
| 41 | + - Open the file `lib/main.dart`. |
| 42 | + - Locate the `appEnvironment` constant. |
| 43 | + - Set its value to `AppEnvironment.development`. |
| 44 | + |
| 45 | + ```dart title="lib/main.dart" |
| 46 | + // Use `AppEnvironment.development` to connect to a live backend API. |
| 47 | + const appEnvironment = AppEnvironment.development; |
| 48 | + ``` |
| 49 | + |
| 50 | +4. **Verify the API Base URL** |
| 51 | + |
| 52 | + - Open the file `lib/app/config/app_config.dart`. |
| 53 | + - Ensure that the `baseUrl` for the `development` case points to the ngrok forwarding URL for your local API server. |
| 54 | + |
| 55 | + |
| 56 | +5. **Run the Application in Development Mode** |
| 57 | + |
| 58 | + With the configuration updated, you can now run the application on your physical device in `development` mode, and it will successfully connect to your local API server. |
| 59 | + |
| 60 | +</Steps> |
0 commit comments