|
1 | 1 | ---
|
2 | 2 | title: "Developing Locally"
|
3 | 3 | sidebarTitle: "Developing Locally"
|
4 |
| -description: "lorem." |
| 4 | +description: "Learn how to receive server events in your local development environment." |
5 | 5 | ---
|
| 6 | + |
| 7 | +## The Problem |
| 8 | + |
| 9 | +When Vapi dispatches events to a server, it must be able to reach the server via the open Internet. |
| 10 | + |
| 11 | +If your API is already live in production, it will be accessible via a publicly known URL. But, during development, your server will often be running locally on your machine. |
| 12 | + |
| 13 | +<Info> |
| 14 | + `localhost` is an alias for the IP address `127.0.0.1`. This address is called the "loopback" |
| 15 | + address and forwards the network request within the machine itself. |
| 16 | +</Info> |
| 17 | + |
| 18 | +To receive server events locally, we will need a public address on the Internet that can receive traffic and forward it to our local machine. |
| 19 | + |
| 20 | +## Tunneling Traffic |
| 21 | + |
| 22 | +We will be using a service called [ngrok](https://ngrok.com/) to create a secure tunnel to our local machine. The flow will look like the following: |
| 23 | + |
| 24 | +<Steps> |
| 25 | + <Step title="Start Our API Locally"> |
| 26 | + We will start our server locally so it is listening for http traffic. |
| 27 | + </Step> |
| 28 | + <Step title="Start Ngrok Agent"> |
| 29 | + We will use the `ngrok` command to start the [ngrok agent](https://ngrok.com/docs/agent) on our |
| 30 | + machine. This will establish a connection from your local machine to ngrok's servers. |
| 31 | + </Step> |
| 32 | + <Step title="Copy Public Ngrok URL"> |
| 33 | + Ngrok will give us a public URL that can receive traffic. We will use this as a server URL |
| 34 | + during development. |
| 35 | + </Step> |
| 36 | + <Step title="Trigger Call Events"> |
| 37 | + We will conduct normal calls on Vapi to trigger events. These events will go to the Ngrok URL & get tunnelled to our local machine. |
| 38 | + |
| 39 | + We will see the event payloads come through locally & log them. |
| 40 | + |
| 41 | + </Step> |
| 42 | +</Steps> |
| 43 | + |
| 44 | +#### Starting Our API Locally |
| 45 | + |
| 46 | +First, ensure that your API is running locally. This could be a Node.js server, a Python server, or any other server that can receive HTTP requests. |
| 47 | + |
| 48 | +Take note of the port that your server is running on. For example, if your server is running on port `8080`, you should be able to access it at `http://localhost:8080` in your browser. |
| 49 | + |
| 50 | +#### Starting Ngrok Agent |
| 51 | + |
| 52 | +Next we will install & run Ngrok agent to establish the forwarding pathway for Internet traffic: |
| 53 | + |
| 54 | +<Steps> |
| 55 | + <Step title="Install Ngrok Agent CLI"> |
| 56 | + Install the Ngrok agent by following Ngrok's [quickstart |
| 57 | + guide](https://ngrok.com/docs/getting-started). Once complete, we will have the `ngrok` command |
| 58 | + available in our terminal. |
| 59 | + </Step> |
| 60 | + <Step title="Start Ngrok Agent"> |
| 61 | + Run the command `ngrok http 8080`, this will create the tunnel with Ngrok's servers. |
| 62 | + <Note>Replace `8080` with the port your server is running on.</Note> |
| 63 | + </Step> |
| 64 | +</Steps> |
| 65 | + |
| 66 | +#### Copy Public Ngrok URL |
| 67 | + |
| 68 | +You will see an output from the Ngrok Agent CLI that looks like the following: |
| 69 | + |
| 70 | +<Frame caption="Terminal after running the 'ngrok' command forwarding to localhost:8080 — the 'Forwarding' URL is what we want."> |
| 71 | + <img src="/static/images/server-url/developing-locally/ngrok-cli-ui.png" /> |
| 72 | +</Frame> |
| 73 | + |
| 74 | +Copy this public URL that Ngrok provides. This URL will be accessible from the open Internet and will forward traffic to your local machine. |
| 75 | + |
| 76 | +You can now use this as a server URL in the various places you can [set server URLs](/server-url/setting-server-urls) in Vapi. |
| 77 | + |
| 78 | +<Note> |
| 79 | + This URL will change every time that you run the `ngrok` command. If you'd like this URL to be the |
| 80 | + same every Ngrok session, look into [static domains on |
| 81 | + Ngrok](https://ngrok.com/docs/getting-started#step-4-always-use-the-same-domain). |
| 82 | +</Note> |
| 83 | + |
| 84 | +#### Trigger Call Events |
| 85 | + |
| 86 | +We will now be able to see call events come through and get logged to our terminal. |
| 87 | + |
| 88 | +Feel free to follow any of our [quickstart](/quickstart) guides to get started with building assistants & conducting calls. |
0 commit comments