DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Breaking Up a Monolithic Database with Kong
  • User-Friendly API Publishing and Testing With Retrofit
  • Diving Deep Into REST API Channels
  • GraphQL vs REST API: Which Is Better for Your Project in 2025?

Trending

  • Building a DevOps-Ready Internal Developer Platform: A Hands-On Guide to Golden Paths, Self-Service, and Automated Delivery Pipelines
  • Feature Flag Debt: Performance Impact in Enterprise Applications
  • LLM-Powered Deep Parsing for Industrial Inventory Search
  • OpenAPI From Code With Spring and Java: A Recipe for Your CI
  1. DZone
  2. Data Engineering
  3. Databases
  4. Python REST API Example (With Microservices) — Part 1

Python REST API Example (With Microservices) — Part 1

Writing REST APIs with Python is an important skill for the microservices world. We'll start learning the steps with a basic class in this tutorial.

By 
Bill Ward user avatar
Bill Ward
·
Aug. 16, 18 · Tutorial
Likes (20)
Comment
Save
Tweet
Share
58.4K Views

Join the DZone community and get the full member experience.

Join For Free

With the transition to microservices, it becomes necessary to know how to write simple REST APIs using Python.

In this post, I give a Python REST API example using Tornado. This is the first post in the series where we will design the microservice and code the sample class that the microservice will manage.

Design

To begin with, let's go ahead and define what out microservice is going to do. For this example, I want to track books. It can add books, remove books and give us a list of all the books. We could add more functionality but I want to keep this simple. This gives us a couple of endpoints:

We are not going to use a database, so we will have no persistence if we kill the web service. Again, I really want to keep this Python REST API example simple.

Our Python REST API Example Code

Now let's get to the code. Let's start out by writing our Book class that we will use to track our books.

import json


class Book:

    def __init__(self):
        self.books = []

    def add_book(self, title, author):
        new_book = {}
        new_book["Title"] = title
        new_book["Author"] = author
        self.books.append(new_book)
        print("Book: {0}".format(new_book))
        return json.dumps(new_book)

    def del_book(self, title):
        found = False
        for idx, book in enumerate(self.books):
            if book["Title"] == title:
                index = idx
                found = True
                del self.books[idx]
        print("books: {0}".format(json.dumps(self.books)))
        return found

    def get_all_books(self):
        return self.books

    def json_list(self):
        return json.dumps(self.books)

This is just a very simple class to let us add, delete and show all books which is a simple list of dicts which contains the book title and author. Notice that we have a method that returns JSON back. We will need this later for our web service to send output back.

In the next part, we will code the actual API itself.

Conclusion

I hope you have enjoyed this article, if so please leave a comment below. Also, please feel free to share the article with your friends. Thanks again for reading this post.

REST API Web Protocols microservice Python (language)

Published at DZone with permission of Bill Ward. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Breaking Up a Monolithic Database with Kong
  • User-Friendly API Publishing and Testing With Retrofit
  • Diving Deep Into REST API Channels
  • GraphQL vs REST API: Which Is Better for Your Project in 2025?

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook