API Builder

Deploy an API Builder Container to Fly.io

Deploy an API Builder Container to Fly.io

In a prior blog post we looked at how to deploy API Builder Docker containers on Railway. In this blog post, we’ll look at another hosting platform, Fly.io that also offers a free tier that is useful for development, testing, and proof of concepts. We’ll walk step by step through how to deploy an API Builder container to Fly.io.

For this blog post, you will need a working API Builder application tested and working in Docker and a Fly.io account. I will use the API Builder Stock Watchlist API project described here.

The basic steps are:

  • Install the flyctl CLI (You can signup for a fly.io using the CLI)
  • Deploy your API Builder project to Fly.io
  • Check the Hostname for your deployed app
  • Test your API Builder API from Postman (or Curl, etc….)

We will describe the steps from start to finish, and when we are done we will have an API deployed in Railway that can be publicly accessed over the internet.

Install the flyctl CLI

Follow the online docs for CLI installation .I used the following command:

curl -L https://fly.io/install.sh | sh

Sign up for an account using:

flyctl auth signup

(I used Github to authenticate)

Sign in to fly.io using:

flyctl auth login

Deploy your API Builder project to Fly.io

Follow the instructions here.

In your API Builder project folder, run the launch command:

fly launch

Accept all defaults but enter N (no) at the ‘Would you like to deploy now?’ step (as shown in the terminal log below)

MacBook-Air-3:watchlist leorbrenman$ fly launch
Creating app in /Users/leorbrenman/Dropbox/Work/Current Projects/App Integration/APIB/API/APIB WatchList API/APIB/watchlist
Scanning source code
Detected a Dockerfile app
? App Name (leave blank to use an auto-generated name):
Automatically selected personal organization: Leor Brenman
? Select region: ord (Chicago, Illinois (US))
Created app holy-tree-1051 in organization personal
Wrote config file fly.toml
? Would you like to set up a Postgresql database now? No
? Would you like to deploy now? No
Your app is ready. Deploy with `flyctl deploy`

You now have two options for your environment variables:

  • Enter your environment variables in the fly.toml file that was automatically placed in your project folder in the [env] section. If you choose to do this, then you will need to put the environment variable values in quotes, otherwise you may get an error on deploy. This method is not recommended because you may check this file in to Github by mistake and your sensitive data will be on Github. Instead use secrets as described next.
  • Set your environment variables as secrets as described [**here**](https://fly.io/docs/reference/secrets/). You can execute the secrets CLI commands for each environment variable as shown below:
flyctl secrets set PORT=8080
flyctl secrets set LOG_LEVEL=debug
flyctl secrets set YAHOO_FINANCE_APIKEY=WA5G......Lep
flyctl secrets set API_KEY=BzX.......Xa

This will not place any entries in the fly.toml so it will be safe to check into Github (even if only by accident).

Deploy using:

flyctl deploy

See the sample terminal log below for reference:

MacBook-Air-3:watchlist leorbrenman$ flyctl deploy
==> Verifying app config
--> Verified app config
==> Building image
Remote builder fly-builder-sparkling-forest-1900 ready
==> Creating build context
--> Creating build context done
==> Building image with Docker
--> docker host: 20.10.12 linux x86_64
Sending build context to Docker daemon  115.7kB
[+] Building 0.5s (9/9) FINISHED
 => [internal] load remote build context                                                                                     0.0s
 => copy /context /                                                                                                          0.1s
 => [internal] load metadata for docker.io/library/node:14-alpine                                                            0.4s
 => [1/5] FROM docker.io/library/node:14-alpine@sha256:4aff4ba0da347e51561587eba037a38db4eaa70e1a6c8334d66779fe963d5be7      0.0s
 => CACHED [2/5] WORKDIR /app                                                                                                0.0s
 => CACHED [3/5] RUN addgroup -S axway-group && adduser -S axway-user -G axway-group &&  chown -R axway-user:axway-group /a  0.0s
 => CACHED [4/5] COPY --chown=axway-user:axway-group . /app                                                                  0.0s
 => CACHED [5/5] RUN npm install --production --no-optional                                                                  0.0s
 => exporting to image                                                                                                       0.0s
 => => exporting layers                                                                                                      0.0s
 => => writing image sha256:069b0603dd7e2c97bb89b60c12892fae657d99bb2555d4ee569dcbe06eb6ecda                                 0.0s
 => => naming to registry.fly.io/holy-tree-1051:deployment-01GCETY7RCRDMPAGN00VMP180E                                        0.0s
--> Building image done
==> Pushing image to fly
The push refers to repository [registry.fly.io/holy-tree-1051]
b69b25375305: Pushed
57389bc3eb21: Pushed
cc5720ff166a: Pushed
4184a6dd75fd: Pushed
b91dc7697d96: Pushed
799bdbca4850: Pushed
f1f6e22747cf: Pushed
994393dc58e7: Pushed
deployment-01GCETY7RCRDMPAGN00VMP180E: digest: sha256:879486794b9fb9a504083bb7225a782d5490145da31cdff285e93a5703d1c240 size: 1995
--> Pushing image done
image: registry.fly.io/holy-tree-1051:deployment-01GCETY7RCRDMPAGN00VMP180E
image size: 176 MB
==> Creating release
--> release v2 created
--> You can detach the terminal anytime without stopping the deployment
==> Monitoring deployment
 1 desired, 1 placed, 1 healthy, 0 unhealthy [health checks: 1 total, 1 passing]
--> v0 deployed successfully

Check app and get host name using:

flyctl status

See the sample terminal log below for reference:

MacBook-Air-3:watchlist leorbrenman$ flyctl status
  Name     = holy-tree-1051
  Owner    = personal
  Version  = 0
  Status   = running
  Hostname = holy-tree-1051.fly.dev
  Platform = nomad
Deployment Status
  ID          = 58c0404b-2926-6e4a-40fa-de1969df2524
  Version     = v0
  Status      = successful
  Description = Deployment completed successfully
  Instances   = 1 desired, 1 placed, 1 healthy, 0 unhealthy
62533984app    0      ord   run    running1 total, 1 passing0       2m1s ago

You can see that my hostname is holy-tree-1051.fly.dev

Test your API

I tested my API using the following CURL command;

curl --location --request GET 'https://holy-tree-1051.fly.dev/api/watchlist?stocklist=aapl,txn,intc,amzn,t' \
--header 'accept: application/json' \
--header 'apikey: Bz.......LXa'

and got the following response as expected:

        "symbol": "AAPL",
        "lastPrice": 156.12,
        "change": 0.1599884
        "symbol": "TXN",
        "lastPrice": 168.33,
        "change": 2.5099945
        "symbol": "INTC",
        "lastPrice": 30.815,
        "change": 0.16500092
        "symbol": "AMZN",
        "lastPrice": 130.12,
        "change": 0.6399994
        "symbol": "T",
        "lastPrice": 16.915,
        "change": 0.045000076

You can check the logs using:

flyctl logs

You can open the Web UI Dashboard for your API Builder API using:

flyctl dashboard

This will launch your browser to the Web UI:

You can also see your logs in the Web UI in the Monitoring tab:

If you no longer need your app deployed, you can remove the app using:

flyctl apps destroy [APPNAME]

in my case:

flyctl apps destroy holy-tree-1051


Fly.io offers a free hosting plan for hosting API Builder (and other) apps. Its CLI makes it incredibly easy to deploy an API Builder app to Fly in a few commands.

Need help with API Builder? Extend your skills with Axway University.