How to Build REST API with SQLAlchemy & Marshmallow

In this article i want to show you How to Build REST API with SQLAlchemy & Marshmallow, basically we are using SQLAlchemy & Marshmallow for Building REST API in Flask. also you can watch the complete video for this article at the end.

 

Also Check this article

Build Complete CRUD Application With FLASK & SQLAlchemy

 

What is Flask ?

Flask is a web framework. This means flask provides you with tools, libraries and technologies that allow you to build a web application. This web application can be some web pages, a blog, a wiki or go as big as a web-based calendar application or a commercial website.

Flask is part of the categories of the micro-framework. Micro-framework are normally framework with little to no dependencies to external libraries. This has pros and cons. Pros would be that the framework is light, there are little dependency to update and watch for security bugs, cons is that some time you will have to do more work by yourself or increase yourself the list of dependencies by adding plugins.

 

What is SQLAlchemy ?

SQLAlchemy is the Python SQL toolkit and Object Relational Mapper that gives application developers the full power and flexibility of SQL.

It provides a full suite of well known enterprise-level persistence patterns, designed for efficient and high-performing database access, adapted into a simple and Pythonic domain language.

 

 

What is Flask-SQLAlchemy ?

Flask-SQLAlchemy is an extension for Flask that adds support for SQLAlchemy to your application. It aims to simplify using SQLAlchemy with Flask by providing useful defaults and extra helpers that make it easier to accomplish common tasks.

See the SQLAlchemy documentation to learn how to work with the ORM in depth. The following documentation is a brief overview of the most common tasks, as well as the features specific to Flask-SQLAlchemy.

 

 

What is Marshmallow ? 

Marshmallow is an ORM/ODM/framework-agnostic library for converting complex data types, such as objects, to and from native Python data types.

 

Marshmallow schemas can be used to:

  • Validate input data.
  • De-serialize input data to app-level objects.
  • Serialize app-level objects to primitive Python types. The serialized objects can then be rendered to standard formats such as JSON for use in an HTTP API.

 

What is Flask-Marshmallow ?

Flask-Marshmallow is a thin integration layer for Flask (a Python web framework) and marshmallow (an object serialization/deserialization library) that adds additional features to marshmallow, including URL and Hyperlinks fields for HATEOAS-ready APIs. It also (optionally) integrates with Flask-SQLAlchemy.

 

 

Installation

 

 

So now this is the complete code for How to Build REST API with SQLAlchemy & Marshmallow

 

 

 

OK in the above code, this is our database configuration iam using mysql database, and also iam using Wamp Server for this.  we are initializing our flask app, also we need to create the object for SQLAlchemy and Marshmallow.

 

 

After that we are going to make one Post class for our posts model which has id , title, description and author as their columns and below we have defined structure of response of our endpoint. We have defined post_schema as instance of PostSchema and posts_schema as instance of PostSchema with multiple fields.

 

 

OK Now we are going to set routes for our endpoint so let’s first make our first end point which has route to “/post” and HTTP method as POST, which can we used for adding new Posts into our database.

 

 

And these routes are for getting the post, updating the post , deleting the post and getting a particular post

 

 

That’s it. We have added all required code for adding , updating , deleting and selecting of our Post model, now your can run your flask application, if you see the video for this article iam using Postman for checking of my endpoints.

 

 

Also you can watch the complete video for this article

 

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Share via
Copy link
Powered by Social Snap
×