paint-brush
How To Build An API Without Coding [Python]by@DavidZisky
1,972 reads
1,972 reads

How To Build An API Without Coding [Python]

by David ZiskyNovember 24th, 2019
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Python Eve is a REST-centered API framework built on top of Flask created by Nicola Iarocci. It’s effortless to build complete API because all You need to kick-start is a MongoDB server and two simple python files (app.py which in most basic setup contains 4 lines of code and settings.py) The only thing is that everything you’ve need to build fully functional API is that you need to define endpoints, database connection and extra configuration if you wish.

Company Mentioned

Mention Thumbnail
featured image - How To Build An API Without Coding [Python]
David Zisky HackerNoon profile picture

I’ve spent the last 10 years of my life as a System Engineer, which means when I first had to build an API, I had no idea how to do it. I knew python quite a bit since as a SysAdmin I often had to write some smaller or bigger scripts, but building the whole API was a challenge for me. Sure — I could just google the phrase and find dozens of tutorials on how to build an API, but all of them were somehow complicated or created for raw developers (which means — with assumptions that you know already all the tools and frameworks that developers use).

Fortunately, at work, I got assigned to a project for a company which had an API prototype written with Python Eve framework. I’ll explain Python Eve in a bit, but it’s not a story about Eve per se. It’s a story about how I made building an API with Python Eve even easier. Shall we?

As I mentioned above — the story begins with Python Eve and as also mentioned earlier — we’re not focusing on Eve today, but I need to give you a bit of a context about it. Eve is a REST-centered API framework built on top of Flask created by Nicola Iarocci .

What’s great about it is that it’s effortless to build complete API because all You need to kick-start is a MongoDB server and two simple python files (app.py which in most basic setup contains 4 lines of code and settings.py which define the endpoints, database connection and extra configuration if you wish - all in one simple file).

Simplicity is the first selling point of Eve, but make no mistake — it does not limit you at all. It is very feature-rich (it gives most of the things you would expect from an API — a full range of CRUD operations, filtering, sorting, pagination, conditional requests, data validation and so on…). Plus since it’s built on top of Flask — most Flask add-ons should work with Eve too.

So, let me quickly show You “how to build an API in 5 minutes” and after that, I’ll show you even faster method ;)

To start with Eve you can simply install it with pip (please use Python3):

pip install eve

If you have Eve installed, you need to spin up a MongoDB database. It all depends on your platform and setup but assuming that you have docker installed you can, for example, use official mongo image:

docker run --name mongo -d mongo

Now, all we need is two small files. One called for example app.py (or run.py, or however you prefer to call it) with the following content:

from eve import Eve

app = Eve()
app.run()

So we simply imported Eve, created new Eve object and called run function. Now, that’s enough to spin up Eve instance, but we need to define all the endpoints, connection to Mongo and so on. For that, we need a file called settings.py (by default Eve expect that file to be called like this, you can change it if you want, but then you need to point to it in you app.py — for simplicity, we keep to defaults).

The content of this file can be customised depending on how many features you want to use, but it must contain the following:

# 1. MongoDB connection details
MONGO_URI = 'mongodb://mongo:27017/evedemo'

# 2. Resource and items REST methods definition
RESOURCE_METHODS = ['GET', 'POST', 'DELETE']
ITEM_METHODS = ['GET', 'PATCH', 'DELETE']

# 3. Data schema
people = {
    'schema': {
        'firstname': {
            'type': 'string',
            'minlength': 1,
            'maxlength': 10,
        },
        'lastname': {
            'type': 'string',
            'minlength': 1,
            'maxlength': 15,
            'required': True,
            'unique': True,
        }
     }
}

4. Endpoints definition
DOMAIN = {
    'people': people
}
  1. MongoDB definition — in the example above I used _URI version but it’s also possible to define MONGO_HOST, MONGO_PORT, MONGO_DBNAME and more options separately
  2. RESOURCE/ITEMS_METHODS — as the name implies defines resource REST methods (so what is allowed to do when you send a request to http://your_api/example and what’s allowed for particular items like http://your_api/example/[uuid]
  3. Schema definition — that’s the core and most time-consuming part of building settings.py file for Eve. Here you need to define a schema for each endpoint. In this example, we have only one schema with only two entries firstname and lastname which are strings. Now, I purposely made it that unrealistically easy because that’s the part we will automate in next step but keep in mind that in real life scenario You would probably have few endpoints with a lot on schema inside each and every single one.
  4. Endpoints definition — in step 3 we created a schema and in DOMAIN section we have to define mapping endpoint <> schema. In our example, we simply create people endpoint which maps to people variable (which we just defined on line 9)
  5. That’s everything you need to build fully functional API. The only thing is that it's very, very basic now. One endpoint with just few keys... to make it useful we would have to create more endpoints and schema which is as simple as defining more keys and their types (step 3) and map them to endpoints.

    But is it really that simple? Well, it is simple but time consuming. If you want let's say 20 different endpoints with dozens of keys in each? Here comes the point of this story — let me introduce you the EveGenie:

    https://github.com/DavidZisky/evegenie

    EveGenie can generate whole settings.py file for You from JSON file with the data you expect to get from the API. What it means is that if you have JSON file generated by some UI, some tool, your colleague or anything else, you can simply run evegenie — it will read it and create schema and endpoint definitions for you (+ MongoDB connection line and RESOURCE/ITEM_METHODS — so the whole settings.py file).

    How to use it then? Just like this:

    python geneve.py sample.json

    NOTE: evegenie is only compatible with Python 3.x

    So, forget about building API in 5 minutes… You can do it in 5 seconds — in the GitHub repo you will also find simple run.py file so the only two things (assuming You have Eve installed, and evegenie repo cloned) you need to do to create fully functional and running API is:

    python geneve.py your_data.json

    and then:

    python run.py

    Keep in mind that this will create the schema and run the API but it won’t have any data inside (the json file you provide for evegenie is only used to create schema — not to load data). But since you have API running you can simply send a POST request with the very same JSON file.

    And that's all! I hope you'll find it useful. Remember - I showed you the way for creating an API in no time which is useful for testing or when you need an API to actually test something else (networking, maybe some kubernetes deployments, etc) but with PythonEve you can create production ready APIs.