“I always say, 'This will be my last 'Metal Gear.'” Hideo Kojima

HOWTO create a Slim API with Heroku


I wanted to test some ideas for a future project that required to have an API up and running. Despite not having any prior interest/experience with web APIs I decided to do some search and read about. At first I thought it was going to be a pain in the ass, but quickly I realized that it may be possible to deploy a basic API using Heroku.


What do we need?

I will cover the procedure using Archlinux, but it sure is similar with other distros (and other OSes like Windows or OSX).

Configuring system

First of all, install everything. In Archlinux, this can easily be done with the help of packer script (since Composer is in the AUR):

packer -S php-composer heroku-toolbelt git

Then, open your /etc/php/php.ini and uncomment the following:

extension phar.so
extension openssl.so

Now you can use Composer to configure your project.

Configuring project

You will find this extremely easy and quick. We will use a custom buildpack that will detect and setup Slim for us. First of all, create a Heroku repo. You can do it from the command-line, through the heroku toolbelt. Like this:

heroku apps:create --buildpack 

You can add “–region eu” if you want Heroku to create your app in Europe. Also, you can specify a name at the end of the command like this:

heroku apps:create --buildpack 
https://github.com/CHH/heroku-buildpack-php my-wonderful-app

Now, create a file named composer.json in the root of your project.

We will use Slim, but Christoph Hochstrasser's custom buildpack can use other frameworks. Please, refer to the link provided if you want to use them.

For Slim we write composer.json like this:

    "require": {
        "slim/slim": "2.*"
    "extra": {
        "heroku": {
            "document-root": "v1",
            "index-document": "index.php"
If you want your API to be located on another path just set the "extra" parameters as you need.

After that, we proceed to install Slim as suggested on its web:

curl -s https://getcomposer.org/installer | php
php composer.phar install

Finally add files to git. For the buildpack to detect Slim you need both composer.json and composer.lock (created just now by Composer). You can leave composer.phar behind. So:

git add composer.lock
git add composer.json
git add v1/index.php

Commit your changes and start working on your API.

Coding the API

Your index.php is where your API should be coded. Just start it like the following:


require '../vendor/autoload.php';

$app = new \Slim\Slim();

There are lots of good tutorials over the net, I will not cover this part because I’m no API expert. Sorry :( But I can refer you to the tutorial that has inspired me and served as a base for this one. Credits to AndroidHive and the author, Ravi Tamada.

Deploy to Heroku

This involves no secret. Deploy your app as usual:

git push heroku <your-current-branch>

Let’s give it a try. If, for instance, your app is named my-wonderful-app, then you can test your API through http://my-wonderful-app.herokuapp.com/endpoint

Any unknown traffic (like the one just mentioned) will be redirected to v1/index.php by the buildpack. No .htaccess needed :)

And that’s it! :) Feel free to ask anything or comment whatever.

Happy coding!