# Set up your Go and Gin development environment

This tutorial covers how to set up your Go and Gin development environment with a sample application and Twilio webhook configuration.

**Time to complete**: Approximately 20-45 minutes

## Prerequisites

Before you begin this tutorial, do the following:

* [Create a Twilio account](https://www.twilio.com/try-twilio) (free tier available).
* [Purchase a Twilio phone number](https://www.twilio.com/console/phone-numbers/incoming).
* [Install Go 1.19 or later](https://go.dev/doc/install).
* [Intsall ngrok](https://ngrok.com/).
* [Install Visual Studio Code](https://code.visualstudio.com/) or another IDE.

## Set up your Go and Gin development environment

Using VS Code, create a directory and configure your Gin settings for your app:

1. Open VS Code.
2. To open the terminal in VS Code, go to **View** > **Terminal**.
3. Create a directory for your project.

   ```bash
   mkdir my-twilio-project
   cd my-twilio-project
   code .
   ```
4. Before starting any Go project, run `go mod init` to create a `go.mod` file for your project:

   ```bash
   go mod init go-example
   ```

   This command creates a `go.mod` file for you, which is helpful for [installing dependencies](https://go.dev/doc/modules/managing-dependencies#adding_dependency) for running an HTTP server, using Twilio, and more.
5. Install the required dependencies for the Gin web framework:

   ```bash
   go get -u github.com/gin-gonic/gin
   ```

### Create a sample Gin application

To test that you configured your development environment correctly, you can use the following code to create a minimally-featured Gin application, and verify its response in a browser:

1. Add a file called `main.go` to your workspace.
2. Add the following code:
   ```bash
   package main

   import (
     "net/http"

     "github.com/gin-gonic/gin"
   )

   func main() {
     r := gin.Default()
     r.GET("/ping", func(c *gin.Context) {
       c.JSON(http.StatusOK, gin.H{
         "message": "pong",
       })
     })
     r.Run() // listen and serve on localhost:8080
   }
   ```
3. Save this file in your workspace.
4. Run your Gin application in the terminal.

   ```bash
   go run main.go &
   ```
5. Open your browser to `http://localhost:8080/ping`. The browser should display:

   ```json
   {"message":"pong"}
   ```

   This verifies that you properly configured your Go and Gin development environment and you can access your app locally.

## Install ngrok for local development

Once you see your sample Gin application's "pong" response, your development environment is ready to go. However, for most Twilio projects, you'll want to install one more helpful tool: [ngrok](https://ngrok.com).

Most Twilio services use [webhooks](https://en.wikipedia.org/wiki/Webhook) to communicate with your application. When Twilio receives an incoming phone call, for example, it reaches out to a URL in your application for instructions on how to handle the call.

When you're working on your Gin application in your development environment, your app is only reachable by other programs on the same computer, so Twilio won't be able to talk to it.

Ngrok is a helpful tool for solving this problem. Once started, it provides a unique URL on the ngrok.io domain, which will forward incoming requests to your local development environment.

To start, head over to the ngrok download page and grab the binary for your operating system: [https://ngrok.com/download](https://ngrok.com/download)

Once downloaded, make sure your Gin application is running, and then start ngrok using the command `ngrok http 8080`.

```bash
ngrok http 8080
```

You should see output similar to this:

```bash
ngrok by @inconshreveable                                                           (Ctrl+C to quit)

Session Status   online
Account          <Your name> (Plan: Free)
Version          2.3.40
Region           United States (us)
Web Interface    http://127.0.0.1:4040
Forwarding       http://6e81-2601-1c0-6100-5087-309b-c292-5e5f-1f.ngrok.io -> http://localhost:8080
Forwarding       https://6e81-2601-1c0-6100-5087-309b-c292-5e5f-1f.ngrok.io -> http://localhost:8080

Connections      ttl     opn     rt1     rt5     p50     p90
                 0       0       0.00    0.00    0.00    0.00
```

Your unique ngrok domain name will be visible on the "Forwarding" line. In this example, it's `https://6e81-2601-1c0-6100-5087-309b-c292-5e5f-1f.ngrok.io`.

If you open your ngrok forwarding URL with `/ping` appended (e.g., `https://your-ngrok-url.ngrok.io/ping`) in your browser, you should see the "pong" response. If everything is working correctly, you should be able to open that domain name in your browser and see your Gin application's "pong" response displayed at your new ngrok URL.

Anytime you're working on your Twilio application and need a URL for a webhook, use ngrok to get a publicly accessible URL like this one.

## Next steps

You're ready to build out your Go application. Learn more with the following resources:

* [SMS developer quickstart](/docs/messaging/quickstart)
* [WhatsApp Business Platform with Twilio quickstart](/docs/whatsapp/quickstart)
* [Programmable Voice quickstart](/docs/voice/quickstart/server)
