is a microblog
home archives search feed blogroll

Host A Rails API On Heroku


Rails, Heroku, Postgres and Postman.

Create a new Heroku app

Visit Heroku’s website. Create a free account and then create a new Heroku app. The app name must be unique. Your endpoint will be [your_app_name_on_heroku].herokuapp.com.

Create a new Rails API project

Navigate to the directory you want your project to be in and run the following command.

rails new [your_project_name] --api --database=postgresql

Edit your database configuration

Navigate to config/database.yml and change the value of the database key under development, test and production to what you want your database to be called.

Create a model

rails generate scaffold User first_name:string last_name:string

Run project locally

Start Postgres.

rails db:create
rails db:migrate

This will create the database and then perform a migration. The migration will turn the User model you generated into a table. You will need to run a database migration every time you perform changes to your models.

rails server -b localhost -p 5600

This will start a rails server locally. The port can be whatever you want, I chose 5600.

Interact with the API

Create a new POST request in Postman to localhost:5600/users. Copy paste the following JSON in the body.


Now create a new GET request to localhost:5600/users/1. You should be able to retrieve what you just sent to the API.

Initialize Git

git init
git add .
git commit -m "Initial commit"

Deploy to Heroku

heroku login
heroku git:remote -a [your_app_name_on_heroku]
git push heroku master
heroku run rails db:migrate

You can interact with your API in Postman just like you did before by changing the endpoint from localhost:5600 to [your_app_name_on_heroku].herokuapp.com.

Posted on 2018-01-16   #rails     #heroku     #postman     #api     #ruby  

← Next post    ·    Previous post →