Creating REST APIs for HipChat commands

Reading Time: 2 minutes

I exist in a world that uses Python to power some pretty awesome sites and internal tools. From time to time there are REST APIs that I keep on hitting, it would be nice to turn one into a custom slash command for HipChat. I’ll walk you through how I did that for an API.

You can find the code for pypples, a small API just does geocoding for a given address – backed by Google. It’s running on Heroku now. The project is pretty simple…


.

|____ LICENSE.txt

|____ Procfile

|____ pypples

| |____ __init__.py

| |____ api

| | |____ __init__.py

| | |____ address_to_geocode.py

| | |____ resources.py

|____ README.md

|____ requirements.txt

|____ runserver.py

pypples/__init__.py starts up a Flask app and pypples/api/__init__.py sets up some defaults for Flask-RESTful, and lets pypples/api/resources.py create a couple REST endpoints (/geo, and /hipchat/geo). I extracted the actual work the API executed into pypples/api/address_to_geocode.py, so the two endpoints in resources.py could share that code. Not much effort to this API deployed to Heroku using the Heroku Toolbelt:


heroku create pypples

git push heroku master

heroku ps:scale web=1

And if you’re new to Python & Heroku, check out Heroku’s Getting Started with Python article.

So now I have two endpoints at https://pypples.herokuapp.com/geo and https://pypples.herokuapp.com/hipchat/geo. Because slash commands in HipChat post data to the endpoint, the second endpoint (/hipchat/geo) accepts HTTP POSTs.

Now I want to hook this up to HipChat, it’s pretty simple. Go to your rooms, login, and find a room that you want to add functionality to. Once there, find the “Integrations” option in the left navigation.

Select BYO Integrations

Find and click on “Build your own integration” and then name your integration. The name isn’t functionally important, it just helps you find the configuration later.

Name your integration

Hit next once you name your integration and scroll on down to create a new slash command. We need the command and a URL to post data to.

Configure the slash command

After this effort, you should be able to hit your slash command in your HipChat room!

slash commands at work

So feel free to use pypples as a template for building APIs you might share between normal REST consumers and HipChat. Good luck doing the same!