Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/devinmatte/your-first-api
A series of basic layouts that allow you to quickly learn how to spin up your own API in various languages
https://github.com/devinmatte/your-first-api
api express expressjs flask java nodejs php python rest-api restful-api slim slim-framework spring tutorial
Last synced: 28 days ago
JSON representation
A series of basic layouts that allow you to quickly learn how to spin up your own API in various languages
- Host: GitHub
- URL: https://github.com/devinmatte/your-first-api
- Owner: devinmatte
- License: mit
- Created: 2018-01-31T18:45:29.000Z (almost 7 years ago)
- Default Branch: python_flask
- Last Pushed: 2019-09-29T22:48:31.000Z (over 5 years ago)
- Last Synced: 2024-11-09T00:30:30.564Z (3 months ago)
- Topics: api, express, expressjs, flask, java, nodejs, php, python, rest-api, restful-api, slim, slim-framework, spring, tutorial
- Language: Python
- Homepage: https://medium.com/@devinmatte/intro-to-apis-67eb36057739
- Size: 15.6 KB
- Stars: 1
- Watchers: 5
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Your First API, in Python
======================Dependencies
------------- Python
- pipTutorial
--------Full tutorial can be found on [devinmatte.me](https://devinmatte.me/tutorial/2018/02/24/intro-to-apis/)
### Getting Started
Let's start with simply making a root route that returns "Hello World!" to the body of the response.
- First run
pip install -r requirements.txt
- Make sure you're using Python3, you may need to use
pip3
instead ofpip
on some systems. - Edit
__init__.py
and add the following route, then runapp.py
to test.
```python
@app.route("/", methods=["GET"])
def root():
return "Hello World!"
```
### Multiple Request Types
Not every request in an API is **GET** in most cases. Often an API allows for you to make requests to change the underlying data in the system. In order to do this, you're going to want to take advantage of **POST**, **PUT** and **DELETE**. Doing so in each framework is fairly easy to define.
```python
# GET Request
@app.route("/", methods=["GET"])
def root_get():
return "Hello World!"
# POST Request
@app.route("/", methods=["POST"])
def root_post():
return "Got a POST request at /"
# PUT Request
@app.route("/", methods=["PUT"])
def root_put():
return "Got a PUT request at /"
# DELETE Request
@app.route("/", methods=["DELETE"])
def root_delete():
return "Got a DELETE request at /"
```
### Using Status Codes
HTTP requests are made up of two parts, the payload, and the status code. The status code tells whether a request was successful, or failed. It also allows for you handle the results of requests based on the code that it provides. Status codes are generally pretty consistent, so if you're confused what code to return, just reference the [spec](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status).
```python
# Return a 205
@app.route("/success", methods=["GET"])
def return_success():
return "This will return a 205 Status Code", 205
# Return a 404
@app.route("/fail", methods=["GET"])
def return_fail():
return "This will return a 404 Status Code", 404
```
### Get Data to Return
Often APIs are stateless, meaning that they get the data from somewhere. You aren't going to be storing data in data structures as your long term storage. Because of that you'll often interact with Databases. Each language works really well with a number of frameworks. I will recommend some good ORMs for each language below, but you should do your research to find one that works for your use case.
Recommended ORMs:
### Continue Learning
If you want to learn more about APIs or any of these frameworks, there are plenty of good resources available.
- [Flask](http://flask.pocoo.org/)