Build your first REST API with Flask and PostgreSQL

You must make sure that the data that you’re adding to the database is valid. Should your application be accessed from different time zones, you have a way to perform date and time calculations. All you need is a UTC timestamp and the destination time zone. The event system generates events that are useful in event-driven programs, but it adds significant overhead. Since you’re not creating an event-driven program, you turn this feature off. Line 11 creates a variable, app, which is the Flask instance initialized by Connexion.

restful api python flask

This should be enough data to allow us to envision some potential research questions without overwhelming us as we focus on the design of our API. As you use other APIs in your research, you’ll develop a sense of what makes a good API from the perspective of a potential user. The primary focus of this lesson is on creating an API, not exploring or using an API that has already been implemented.

PUT Request

The delete is pretty straight forward it uses the ‘delete_one’ method, which we pass a filter containing the ID of the To-Do we want to delete. Again because this method can be used to delete we return the number of documents deleted. Now that we have a reference to our database we can create a new collection within it. I will be using PyCharm for creating this project, I find it one of the best IDEs for full Python projects but you can, of course, use any IDE that you are comfortable with. In the Rust version of this code, I was nudged to use a different approach.

  • The repo object is an instance of our ToDoRepository class so that we can call our CRUD methods for interacting with our database.
  • Lines 3 to 6 import the built-in pathlib as well as the third-party libraries connexion, SQLAlchemy, and Marshmallow.
  • GET single To-Do request in PostmanIn the image above you will notice that we received a single object back and that it is not part of a list like the previous GET request.
  • In a more complex system this function could check a user database, but in this case we just have a single user so there is no need for that.
  • Open the POSTMAN API tool and hit on each HTTP method request.

The source, or zero time, is a line running from Earth’s north to south pole through the UK. This is the zero time zone from which all other time zones are offset. By using this as the zero time source, your timestamps are offsets from this standard reference point. This field must be unique because you’re using lname as the identifier for a person in a REST API URL. Config.py gets the necessary modules imported into the program and configured. This includes Flask, Connexion, SQLAlchemy, and Marshmallow. Lines 8 and 9 iterate over people and print out the data of each person.

Creating Web APIs with Python and Flask

In this article I’m going to show you how easy it is to create a RESTful web service using Python and the Flask microframework. To set up a Python server, you need to install Python, I would suggest any version above 3.7 as of the year 2019.

restful api python flask

The response of the delete method is the number of documents deleted from the database so we can use this to check if the delete was successful or not. If the number of deleted documents is greater than or equal to one it was successful and we can return a HTTP status code of 200, if not we will return a 404. When I started writing this post, I though it would end up being relatively straightforward. As I added the https://remotemode.net/ requirement that the code should not just be a toy example, some of the inherent difficulties with building a robust API on top of any database became apparent. In this tutorial we will run a basic api using flask-restful, it will only have two routes which will be a get and post method for the purpose of demonstration. Here I have decided to include the name of the application and the version of the API in the URL.

What is REST API?

This displays the structure of the expected response, the content-type of that response, and the description text that you entered about the endpoint in the swagger.yml file. Any time the configuration file changes, the Swagger UI changes as well. Currently you have a REST API running with a single URL endpoint. Your Flask app knows what to serve based on your API restful api python flask specification in swagger.yml. Additionally, Connexion uses swagger.yml to create API documentation for you. In line 7, you tell the app instance to read the swagger.yml file from the specification directory and configure the system to provide the Connexion functionality. The Swagger configuration file is a YAML or JSON file containing your OpenAPI definitions.

restful api python flask

Alternatively, clients will be able to specify a term and we’ll respond appropriately. POST /api/room will let clients send us the room name, and we will create a room in the database against which we can store temperatures. Next, change the list_programming_languages() function to act upon the query parameters before_year and after_year. Flask restful defines the resource class, which contains methods for each HTTP method.

The database was queried to retrieve all the users and stored in the users variable. We looped through both the users variable and each user record in the user_details variable. Each user_details is appended to the all_users variable which we returned to the user in the updated response message. The user_data variable stores the request body, which can be accessed by other variables.

  • We will use this method to retrieve the all the users that we have in out database.
  • This means that the data will be saved to your disk and will exist between runs of the app.py program.
  • This iteration of our API also allows for filtering by more than one field.
  • As you use other APIs in your research, you’ll develop a sense of what makes a good API from the perspective of a potential user.
  • Finally, you instantiate two schemas, person_schema and people_schema, that you’ll use later.
  • Since you’re not creating an event-driven program, you turn this feature off.
  • The obvious way to do that is to build a small database, but because databases are not the topic of this article we are going to take a much simpler approach.

About the Author

You may also like these

No Related Post