Manage API with WSO2 API Manager - API Gateway 🌁

I believe microservice architecture is not a weird sound with many developers nowadays🤔. And one or many times, when you guys hear about the microservice, you may listen to some components name API Gateway as well, or somebody calls it API Manager😕. And you know, it is actually essential in today's architecture. It is almost the first thing people must consider when they want to build a microservice architecture. Let's figure out how important it is and how it works😉.

What is Microservice architecture?

At first, let's get a concise brief of microservice architectures.

As a quotation from google cloud:

Microservices architecture (often shortened to microservices) refers to an architectural style for developing applications. Microservices allow a large application to be separated into smaller independent parts, with each part having its own realm of responsibility. To serve a single user request, a microservices-based application can call on many internal microservices to compose its response.

😵‍💫 Seems to be bitter to swallow this hypothesis from google, right?
In simple, instead of developing everything inside one massive project, including many functionalities like user log-in, shopping cart, and product management for a website, all these functions refer to only one database. Now, we separate them into different services, and each service can be developed like a separate project by an independent team. It runs like an isolated application with its own database and dependencies, and we can do the restarting, upgrade, or revert the version of each service without interrupting the other services. 😊 

If you are still like @.@ 😖, refer to my header picture, which is more than a thousand words 😂.

Now go to API Gateway (API Manager, or whatever!)

A quotation from Redhat:

An API gateway is a way to decouple the client interface from your backend implementation. When a client makes a request, the API gateway breaks it into multiple requests, routes them to the right places, produces a response, and keeps track of everything. An API gateway is one part of an API management system.

And now, turn to my explanation: your UI only needs to know the address of API Gateway when sending the request; API Gateway will route your requests to a suitable service; refer back to my header picture. 😂

Because of that, it helps backend developers relieve them from thinking about dependencies when developing API, helps UI developers no need to identify an address of each API service, and everybody knows where to look for a specific API when needed.

WSO2 API Manager

A definition from WSO2:

WSO2 API Manager is a fully open-source API management platform. It supports API designing, API publishing, lifecycle management, application development, API security, rate limiting, viewing statistics of APIs, and connecting APIs, API Products, and endpoints.

In other words, it is an API Gateway developed by WSO2, which they named API Manager. 😉

Time for practice 🥳

Start up a local WSO2 API Manager.

The simplest way, and the way I like the most, is starting with a docker container. A container helps us start up a service in the cleanest way. Once we don't want that service, just stop and remove the container.

You need to install docker on your local machine first and then start up WSO AM (API Manager) by using the below command:

docker run -d -t \
       -p 9443:9443 \
       -p 8243:8243 \
       -p 8280:8280 \
       wso2/wso2am

Wait for a short while; once the service is ready, you will be able to browse the URL: https://localhost:9443

User name and password are: admin/admin

Tada🎉🎉. Once logged in, you will see a UI like the one below:


Let's check out the process of building an API on an AM.



  1. Creating and publishing an API via the Publisher Portal of WSO2 API-M.
  2. Deploy the API in a Gateway environment.
  3. Publish the API in the Developer Portal.
  4. Subscribing to the API via the Developer Portal of WSO2 API-M and generating keys.
  5. Invoking the API with the generated keys.

For detail of the steps, you can refer to the document of WSO2 (API Management - WSO2 API Manager Documentation 4.1.0); I will not repeat again here. 😊

Make a simple application

Python code

Now I will make a simple application to receive the API request from the API Manager in python; let's discover how to enable that function in python. 👍

It is super short and only includes one API handler:

import os
from os.path import exists
from flask import Flask

VERSION = '0.0.1'
HOST = ''
PORT = 0

app = Flask(__name__)


def get_env_var():
    print('Debug: Get env variables')
    global HOST, PORT
    HOST = os.environ.get('HOST', '0.0.0.0')
    PORT = int(os.environ.get('PORT', 5000))


# curl -L -X GET 'http://localhost:5000/api/v1/greeting'
@app.route('/api/v1/greeting', methods=['GET'])
def request_all_ip():
    print('Debug: request received')
    ret = {'data': 'Greeting from helloAPI'}
    return ret


if __name__ == '__main__':
    get_env_var()

    from waitress import serve
    print('Debug: Start up server')
    serve(app, host=HOST, port=PORT)

The HOST and PORT can be set from the environment variables. In my snippet code, I made them 0.0.0.0 and port 5000.

Let's make a test


Great 😊

Run it in a Docker container

Because my wso2-am is running in a Docker container, to make it able to call this API, I have to put it in another container in the same network as the wso2-am container.


john@L-SGP010078:~$ docker run -it ubuntu:22.04 bash
root@18e8ab5c7aca:~# apt update && apt install -y python3 && apt install -y python3-pip
root@18e8ab5c7aca:~# pip install flask waitress

From the host, copy the source file into the container

john@L-SGP010078:~/work/02-src/03-scripts/python/embedcoder/helloAPI$ docker cp \
main.py competent_shockley:/root/

Test in the container


Nice 😊

Now, we need to note the IP address of this container.


Publish and subscribe an API with the path to my python script



Try out the API on WSO2 AM


Yes 👍

Now try out the curl command.


Hura Hura 🎉🎉🎉

We already have a simple system running with an API Gateway and a service to consume the requests from the Gateway.

You can discover yourself to learn about many features that an API Gateway like WSO2 AM can support regarding your API. Have fun 😉

See you in the next post. 🙋‍♂️🙋‍♂️

#API #APIGateway #SolutionArchitect #DevOps #Cloud





Post a Comment

Previous Post Next Post