How to build an API without coding
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 actual developers (which means — with assumptions that I know quite a lot concepts of programming already). Fortunately, at work, I got assigned a project for a company that had an API prototype written with the Python Eve framework. I’ll explain Python Eve in a second, but it’s not a story about Eve per se. It’s a story about how I made building an API with Python Eve dead simple. Shall we?
As I mentioned above — the story begins with Python Eve and as also mentioned earlier — we’re not focused on Eve today, but I need to give you a bit of 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 a complete API because all you need to kick-start is a running MongoDB server and two simple python files (app.py which in the most basic setup contains 4 lines of code and settings.py which define the endpoints, database connection and extra configuration if you wish). 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 a faster method ;)
To start with Eve you can simply install it with pip:
pip install eve
NOTE: please use Python 3
If we have Eve installed, we need to spin up a MongoDB database. How to do it will depend on your platform and your setup. But assuming that you have docker installed you can, for example, use the official mongo image:
docker run --name mongo
-p 27017:27017
-d mongo
Now, all we need is two small files. An 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 a new Eve object and called run function. And that’s enough to spin up an Eve instance, but we need to define all the endpoints, connections 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 your app.py — for simplicity, we keep to defaults). The content of this file can be customized 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
}
- MongoDB connection — 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
- RESOURCE/ITEMS_METHODS — as the name implies this defines resource REST methods (so what is allowed when you send a request to http://your_api/example and what’s allowed for particular items like http://your_api/example/[uuid]
- Schema definition — that’s the core and most time-consuming part of building the 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 keys
firstname
andlastname
which are strings. Now, I purposely made it that unrealistically easy because that’s the part we will automate in the next step but keep in mind that in real life scenario you would probably have a few endpoints with a lot of keys inside every endpoint. - Endpoints definition — in step 3 we created a schema and now, in the DOMAIN section, we have to define mapping endpoint <> schema. In our example, we simply create a
people
the endpoint which maps topeople
variable.
That’s everything you need to build fully functional (apart from the fact it’s almost useless but to make it useful we would just have to create more endpoints and schema which is simply defining data names and types (step 3) and map them to endpoints. And here comes the point of this story — let me introduce you EveGenie!
EveGenie can generate a whole settings.py file for You by providing a JSON file with the data you expect to get from the API. What it means is that if you have a JSON file generated by some UI, some tool, your colleague or anything else you can execute evegenie — it will read it and create schema and endpoint definitions for you (+ MongoDB connection line and RESOURCE/ITEM_METHODS — so the whole 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 a simple run.py file so the only two things (assuming You have eve installed, MongoDB running and evegenie repo cloned) you need to do to create a fully functional and running API is:
python geneve.py your_data.json
python run.py
Keep in mind that this will create a schema and run the API but it won’t have 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 now simply send a POST request with the very same JSON file.
Having the ability to create API this way is not a solution for developers — while Eve still has a lot of features and you can definitely build very rich production grade API with it — I showed you only the most basic usage of Eve because I use it as a System Engineer for quick microservices/networking testing. I don’t use this solution to create an API on its own— I use this to create an API that will help me debug issues with Kubernetes networking, tests of Istio, etc. But the point is — if you always wanted to try something that required running API and you couldn’t do it because you are not a developer, or perhaps you are even a frontend developer and want to start moving towards a full-stack role — hopefully that was an easy solution for you to achieve your goals.
Best regards!