How to Deploy Your Go API on Elestio
Tags: elestio, go, api
Published: 2023-11-09 08:16:27

---
title: How to Deploy Your Go API on Elestio
published: true
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yxllv2i39ar7krk2fc1x.png
date: 2023-11-08 14:00:33 UTC
tags: Elestio, Go, API
canonical_url: https://blog.elest.io/how-to-deploy-your-go-api-on-elestio/
---

As a developer, you might have built multiple APIs and integrated them with your web application. So, In today's tutorial, we are going to see how you can quickly build a simple Ping-Pong API and deploy it easily and quickly on Elestio.

## Let's set it up!

I presume you already have an API ready that you want to deploy. But for this tutorial, I will start you through the steps from the very beginning so feel free to skip some steps (But remember not all) as your codebase will need some changes to make your deployment successful. Let's start with creating a GitHub repository for your API

![How to Deploy Your Go API on Elestio](https://cdn.hashnode.com/res/hashnode/image/upload/v1699516602210/c5356b18-c6cd-416f-8746-11cd6871235a.png)

![How to Deploy Your Go API on Elestio](https://cdn.hashnode.com/res/hashnode/image/upload/v1699516603495/ae679b41-fc40-4fd5-9d46-133769bdbcde.png)

Next, we will go ahead and open this newly created repository in GitHub Codespaces. As I said if you already have the repo I recommend you use GitHub Codespaces or the code editor where you have already set your project

![How to Deploy Your Go API on Elestio](https://cdn.hashnode.com/res/hashnode/image/upload/v1699516605024/a9d13f25-3bba-4ffe-b0c6-63c4ee0c8098.png)

## Building your Go API

This is an optional step, if you already have an API, please feel free to skip this Once the codespaces are launched, head over to the terminal and enter the following command that will create a `.mod` folder in the repository

```bash
go mod init github.com/<username>/<repo-name>

```

![How to Deploy Your Go API on Elestio](https://cdn.hashnode.com/res/hashnode/image/upload/v1699516606438/cc13cce0-6192-4f63-9dc7-adefcac6f28f.png)

Next, create `main.go` file in the root directory

![How to Deploy Your Go API on Elestio](https://cdn.hashnode.com/res/hashnode/image/upload/v1699516607615/6d018443-a0e9-4d4f-ac67-bb190f286ef7.png)

Inside `main.go` file add the following code. In this tutorial, I won't be explaining the code if you want that let me know in the comments and I will get you a dedicated tutorial on it.

```go
package main

import ( 
    "fmt" 
    "github.com/gin-gonic/gin" 
    "net/http" 
)

func main() {
    fmt.Println("Welcome to the Ping-Pong API")

    //Initializing the gin package 
    router := gin.Default()

    //Endpoint we are going to use today 
    router.GET("/ping", handlePing)

    //Server runs on everywhere IP and on 8080 port 
    router.Run("0.0.0.0:8080") 
}

//Function to handle the /ping endpoint 
func handlePing(g *gin.Context) { 
    g.IndentedJSON(http.StatusOK, gin.H{"message": "Pong!"}) 
}

```

After you have added the code `main.go` file, head over to the terminal and run the following command. It will create a `.sum` file with all the required libraries mentioned in `.mod` the file

```bash
go get .

```

![How to Deploy Your Go API on Elestio](https://cdn.hashnode.com/res/hashnode/image/upload/v1699516609549/4d6a0a13-930a-4772-8579-551dba608c04.png)

After this, you can try out running your API and testing the `/ping` endpoint using curl or the "Open in browser" option

![How to Deploy Your Go API on Elestio](https://cdn.hashnode.com/res/hashnode/image/upload/v1699516610901/149a57c0-3c80-4248-94eb-47280cd26560.png)

## Deploying your API

Go ahead and create another file called `elestio.yaml` in your root directory and add the following code to it

```yaml
config:
  runTime: "GO"
  version: ""
  framework: "GO"
  buildCommand: "go build -o main"
  buildDir: "./"
  runCommand: "./main"
ports:
  - protocol: "HTTPS"
    targetProtocol: "HTTP"
    listeningPort: "443"
    targetPort: "8080"
    targetIP: "172.17.0.1"
    public: true
    path: "/"
    isAuth: false
    login: ""
    password: ""
exposedPorts:
  - protocol: "HTTP"
    hostPort: "8080"
    containerPort: "8080"
    interface: "172.17.0.1"
environments:
  - key: "ENV"
    value: "production"
webUI:
  - url: "https://[CI_CD_DOMAIN]/ping"
    label: "Website"

```

This file states the configurations for your applications like the build and run commands you want to run to start your go API, then ports. Here the reverse proxy configuration is listening to the port `443` of the public IP address and the port `8080` is the port exposed by the Docker containers. All the traffic to the port `443` will be redirected to the port `8080`. Environment variables are stated with the `webUI` link you want to show to quickly access the API from the Elestio dashboard.

After adding this file, commit your code to the repository.

Now go to [Elestio](https://elest.io/?ref=blog.elest.io), and login and set up the account if you don't already have one set up.

Click on the "CI/CD" section from the left panel select your repository host and the repository that has your Api and click on "Import"

![How to Deploy Your Go API on Elestio](https://cdn.hashnode.com/res/hashnode/image/upload/v1699516612413/3659f6a5-9957-4452-a8e8-177e4db6153a.png)

Now select your preferred cloud service provider

![How to Deploy Your Go API on Elestio](https://cdn.hashnode.com/res/hashnode/image/upload/v1699516613594/aa391244-c0f6-44a5-8f99-a2eb0ccb8550.png)

Give the deployment a name of your choice (You can keep the default name too)

![How to Deploy Your Go API on Elestio](https://cdn.hashnode.com/res/hashnode/image/upload/v1699516614849/5cd736e0-92dc-438e-92d5-7124ade9bba0.png)

Add a name to your project and Create a CI/CD pipeline

![How to Deploy Your Go API on Elestio](https://cdn.hashnode.com/res/hashnode/image/upload/v1699516616086/3ca3ae9d-cbe8-423b-b03a-7546be9ad98a.png)

Awesome! You have successfully created a CI/CD pipeline that gets your API to the VMs.

Moving next, click on the "Services" Tab on the left and click on the service you just created.

![How to Deploy Your Go API on Elestio](https://cdn.hashnode.com/res/hashnode/image/upload/v1699516617581/fbdd8026-8152-411c-9e3f-1a68ff7dac17.png)

Now check all the information on the overview if you want else head over to the Pipeline tab

![How to Deploy Your Go API on Elestio](https://cdn.hashnode.com/res/hashnode/image/upload/v1699516618756/dae83d72-0238-413a-93ba-8e8d80000932.png)

After heading inside you will find an option "Website" Click on it and head over to the link provided in it. It's the API endpoint.

![How to Deploy Your Go API on Elestio](https://cdn.hashnode.com/res/hashnode/image/upload/v1699516620051/3476644f-90c9-4613-b331-7db4f3a31111.png)

![How to Deploy Your Go API on Elestio](https://cdn.hashnode.com/res/hashnode/image/upload/v1699516622043/1513a438-a970-4492-82ca-ecbf2afc7bab.png)

Note that because we only had `/ping` endpoint, I appended it to the URL provided in the configs. In the general scenario as you have multiple endpoints you can choose the URL to not append `/ping` and change it to `/`

As mentioned above if you have multiple API endpoints then it's a bad idea to add them to the URL, you can remove the `/ping` to get displayed in the URL by updating the `elestio.yaml` in your code to the following

```yaml
#Before webUI: - 
url: "https://[CI_CD_DOMAIN]/ping" label: "Website" 
#======================================================= 
#After webUI: - 
url: "https://[CI_CD_DOMAIN]/" label: "Website"

```

## Accessing your API

You can directly click on the link provided in the dashboard and you will see your response as follows

![How to Deploy Your Go API on Elestio](https://cdn.hashnode.com/res/hashnode/image/upload/v1699516623321/e9c3ad8d-74fd-4bdb-9bb6-007e51a10b1b.png)

or you can make a curl request from the terminal in your local computer like

```bash
curl <url-from-the-dashboard>/ping

```

## Where to go from here?

You can now successfully go ahead and insert this URL into your frontend application and fetch the responses from the new hosted API endpoints. Additionally, you can integrate your Go API with a couple of other databases (again all of them are present on Elestio to easily configure).

More tutorials around the same will be published to help your Go API integrate seamlessly with different databases and effortlessly host them onto Elestio

## Thanks for reading 

If you like the blog, then do try out creating your pipelines and if you want to support us then you can add the "Deploy on Elest.io" button to your open source project. Well, that's it for this blog, we will catch up with you in the next one 👋

![How to Deploy Your Go API on Elestio](https://cdn.hashnode.com/res/hashnode/image/upload/v1699516624486/8e7dd14a-0ce4-475b-87eb-9a8fa0f8be8e.png)