REST API for Your bit.io Repository

❗️

This Page Is for an Old Version of bit.io

Some of the information may be out of date. If you have questions, please get in touch at [email protected]

If you plan on providing access to your bit.io repository in a more structured way to clients, one of the ways to consider is making a custom REST API directly to your repository. Although this requires a little more work than directly connecting to repository via bit.io's API, it offers you a lot of power in setup.

I'm going to build this REST API in Python 3, Flask and Flask-RESTful. However, our reference documentation covers how to connect to bit.io using cURL, Ruby, Node, and PHP, so feel free to follow along in the language/technology of your choice.

First, I'm going to get started by creating a new repository on bit.io, with some sample sales data from Kaggle.

800

Next, create a new file server.py on your computer. This will hold our server's code. Making sure you have Flask and Flask-RESTful installed, use the following boilerplate code:

from flask import Flask
from flask_restful import Resource, Api

app = Flask(__name__)
api = Api(app)

class HelloWorld(Resource):
    def get(self):
        return {'hello': 'world'}

api.add_resource(HelloWorld, '/')

if __name__ == '__main__':
    app.run(debug=True)

Running python server.py should start running a server (make sure you are using the right version of Python):

bitio-demo ➤ python3 server.py                                                                            
 * Serving Flask app 'server' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 451-073-227

Great! If we make a cURL request to the server, you should be able to see the hello world output:

bitio-demo ➤ curl "http://127.0.0.1:5000/"                                  
{
    "hello": "world"
}

Now to connect to our repository. Lets download the Python SDK from bit.io: pip install bitdotio. The docs for the Python SDK are here (Note that you need PostgreSQL downloaded for the install to work). Let's snag the API key from the Connect button on our repo:

2028

And we can connect to the repo from our server using just a few lines.

import bitdotio

# Connect to bit.io
b = bitdotio.bitdotio("<YOUR_API_KEY>")

Let's create a new endpoint to return the count of records in our repo (and remove our HelloWorld endpoint). Now our code looks like this:

from flask import Flask
from flask_restful import Resource, Api

import bitdotio

# Connect to bit.io
b = bitdotio.bitdotio("<API_KEY>")

app = Flask(__name__)
api = Api(app)

class Count(Resource):
    def get(self):
        conn = b.get_connection()
        cur = conn.cursor()
        cur.execute('SELECT count(*) FROM "tk-bitio/Sales"."sales_data_sample";')
        return cur.fetchone()

api.add_resource(Count, '/count')

if __name__ == '__main__':
    app.run(debug=True)

And sure enough, if we make a request to /count, we get:

bitio-demo ➤ curl "http://127.0.0.1:5000/count"                             
[
    2823
]

and that's it! We've created a Flask server to serve resources of your bit.io repository. You can add fields, remove fields, or restrict access to your repo in a very controlled way using this approach.

What did you think of this blog post? Was it helpful and/or was something confusing? Please let us know in the Discussions tab!