Deploying API in Adonis JS on Heroku

Ronaldo B.July 27, 20225 min read

In this article

In the last article, we learned how to create an API using Adonis JS and PostgreSQL. You can access this content by clicking here.

But how can we deploy this project and make it publicly available? This is what we will do in this article. We will deploy this API on Heroku, a well-known cloud service platform used to host web applications.

It offers many advantages, such as a free plan to host our projects. Additionally, it has integration with GitHub, which greatly facilitates the deployment process.

Before we start configuring Heroku, we will need to make some adjustments to our project.

Additional configurations in our code

The first change we will make will be in the package.json file. We will change the “start” script. This is necessary because this script is what Heroku will execute to start our project. It will look like this:

"start": "ENV_SILENT=true node server.js"

To explain: one of the primary configurations for the proper functioning of our Adonis JS application is the environment variables. These variables are defined in the .env file. However, this file will not be added to our GitHub. Therefore, Adonis will not be able to find such variables and will return an error. To prevent this from happening, we set ENV_SILENT to true, so that the variables are ignored and the credentials we will define in Heroku shortly are taken into account.

The next resource we will add to our code is the “Procfile” file. It is the file for Heroku configurations and defines the commands that will be executed when our server starts.

It will be located at the root and will have the following content:

release: ENV_SILENT=true node ace migration:run --force
web: ENV_SILENT=true npm start

With these resources added to our project, we are ready to start our project on Heroku.

Starting configuration on Heroku

To configure our project, we need to have an account created on Heroku. Then we can access the console page and click the “New” button > “Create new app”:

Option to create a new project on Heroku

On the next page, we will give a name to our project and click “Create app”:

Page to create a new project on Heroku

The project's homepage will look like this:

Homepage of our project

The first configuration we will make is to install the PostgreSQL service on our server, so that Adonis can access the Database and perform all the operations we created in the last article.

For this, we will click on the second option in the main menu, “Resources”.

On the following page, we will see an area for our project's Add-ons. They are like external resources that are necessary for our project to function properly.

We will click on the “Find more add-ons” option:

Option to select more add-ons for our project

On the next page, we will click on the Add-on “Heroku Postgres”:

Postgres add-on on Heroku

We will be redirected to the page of this Add-on. We will click on the “Install Heroku Postgres” button:

Installation option for Heroku Postgres

To complete the installation, we will fill in our Heroku project name in the input field that will appear on the next page and then click “Provision add-on”:

Final page for Heroku Postgres installation

We will be redirected to the Overview page of our Database. We will click on its name and open its information in a new tab:

Opening database settings in a new tab

This page will show us some data about our Database. We will click on the Settings option:

Overview of our Database

In this next tab, we will click on “View Credentials...”:

Option to view database credentials

The credentials will be displayed to us. They will vary from one Heroku account to another:

Credentials for our Database

Now that we know the credentials, we will add them to the environment variables of our project in Heroku. For that, we will go back to the homepage of our project and click on “Settings”:

Settings page in our Heroku project

We will click on the “Reveal Config Vars” option, which are our environment variables:

Option to reveal environment variables of our application

Now we will indeed add our application credentials to Heroku. The first configuration is the APP_KEY, which we can find in the .env file of our API:

APP\_KEY information in our .env file

This information will also vary from one application to another. We will copy this information and add it to Heroku, always following the key and value format:

Adding environment variable to the project

Note that we follow the pattern of having the configuration name in uppercase letters. This will be repeated for the other configurations. We will also add the environment variables DB_CONNECTION, DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE, and DB_PORT. Our environment variables will look like this:

Configurations of our application in Heroku

With the configurations finalized, it's time to connect our project to GitHub. To do this, we will create a repository on GitHub and then establish the connection between these two servers.

To make this connection, we will select the third option in our main menu, the “Deploy” link. Then, we will select the option to connect with GitHub:

Option to connect the project with GitHub

After making the connection, we need to know the name of our repository on GitHub, which can even be found in the URL of our browser:

How to find the name of our repository through the GitHub URL

We will type this name in the search field of Heroku and then click “Search”:

Searching for our repository on GitHub

Our repository will be displayed and then we will click “Connect”:

Connecting our GitHub repository to Heroku

Some options will be displayed. If you wish, you can click the “Enable Automatic Deploys” button. This way, whenever we make an update on our GitHub, it will be automatically added to Heroku:

Option to enable automatic deploys on Heroku

However, now we will manually deploy. To do this, we will click the “Deploy Branch” button, which will be near the end of the page:

Option for manual deployment on Heroku

At this moment, our migrations will be executed on Heroku and the deployment will actually take place.

At the end of the process, we will see a success message stating that our application is already available for access:

Success message informing that the application is now available

By clicking on “View”, we will be directed to our API in production.

The first time we may not see any data, as our API will be starting in the cloud. We can add some information using Insomnia by providing the URL of our API:

Adding data to our API via Insomnia

Now, when accessing the route that lists all courses in our API in the browser, we will see the following result:

List of courses in our API on Heroku

Excellent, our API is working, now in production!

Conclusion

In this article, we saw how we can deploy an API in Adonis JS on Heroku. If you have a personal project and want to take it to production, Heroku is an excellent option.

If you wish, you can check this same content in video format on our YouTube channel.

You can send suggestions for topics for our Blog by clicking here.

We hope you enjoyed the content and see you in the next article :)

Keep learning with Hcode

Sign up for the course platform and follow the next blog guides.