Code Monkey home page Code Monkey logo

your-first-api's Introduction

Your First API, in Python

Dependencies

  • Python
  • pip

Tutorial

Full tutorial can be found on devinmatte.me

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 of pip on some systems.
  • Edit __init__.py and add the following route, then run app.py to test.
@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.

# 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.

# 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.

your-first-api's People

Contributors

devinmatte avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

ero2000 kn4785

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.