Kubernetes with OpenShift World Tour: Get hands-on experience and build applications fast! Find a workshop!

Use API Connect with a Python web application

With the API Connect service, you can easily serve your APIs over a secure developer platform. Traditionally, developers prepare their APIs for a web service then implement or develop another service for serving APIs in a secure way. API Connect allows developers focus on APIs while it does the rest of the work. API connect can:

  • Categorize APIs,
  • Limit APIs with certain restrictions,
  • Allow identifying different roles on APIs,
  • Allow managing different web services together,
  • Allow developers to version APIs,
  • Give a graphical programming interface for manipulation,
  • Create sample code examples for different languages and tools like cURL, Node.js, and Python.

Learning Objectives

This guide explains:

This study explains how to:

  • Create a Python web app with boilerplate code
  • Use REST APIs over Python
  • Serve your Python REST APIs with API Connect

Prerequisites

In order to use this how-to, you need the following prerequisites:

Estimated Time

Assuming you have an IBM Cloud account setup, this guide should take around 30 mins to complete.

Steps

Creating Python Flask Web App Boilerplate

Let’s start our how-to with preparing example web application. We will use the Python Flask Web Starter boilerplate for that purpose. Open you IBM Cloud catalog and write python in the search bar. Select Python Flask Application. 1. Give it a name and create the application

create

Wait for the application to start and now you should have a python application up and running. See the application by clicking on Visit App URL or by going to https://<application-name>.mybluemix.net.

Once the application is open, you will see an example landing page like the one seen below:

welcome

You can also try some of the other APIs available by default in the boilerplate app. Navigate to:

  • https://<application-name>.mybluemix.net/api/people: Returns a templated list of people returned by the server.
  • https://<application-name>.mybluemix.net/api/people/person1: Returns back a string, Hello person1, where person1 is a variable name you can pass to the server.

Set up API Connect Service

To create an API Connect service, go to the IBM Cloud catalog and search for API Connect, click the only result. When prompted to create the service, give it a name and go ahead with provisioning the service.

create-service

Configuring an API Connect endpoint

We now need to configure API Connect, let’s add a new API by launching the API Connect tool and selecting the Add button.

add-api

Select New API from the drop down menu.

new-api

Now fill in the API details, specifically, the Base Path should be /api/people.

api-details

Once the new API has been created you should see a bunch of new configuration options on the left side menu bar.

api

Let’s select the Path option from the menu and add a new entry called /api, with the same name and path.

path

Also, ensure that the security option is enabled, For this example we will enable the option to use an API key in the header.

security

Save any changes using the disk icon on the top right.

save-valid

Now we head over to Assemble menu and click on the Play button.

assemble

In the tab that opens on the right, enter the application URL (ex. https://<application-name>.mybluemix.net/api/people), set the method as GET, and choose No Cache for the Cache type.

assemble assemble

Ensure the changes are saved. We’ve now completed the minimal amount of configuration necessary for API Connect.

Creating an API Connect product

Head back to Drafts section and select Products to setup a new API Connect product, which is a collection of endpoints.

add-product

Give the new product a name, such as python-app and click Create product

new-product

Once created, head over to the API section in the left hand menu and add an API. Select the API we created in the previous step and select Apply.

connect-api

Save any changes by clicking the disk icon.

save-deploy

We’ve now created our API Connect product, our last step is to publish our APIs.

Publish APIs to the web

Head over to the Dashboard and select Sandbox.

dashboard

Under Products, find the product you created in the previous step.

prod

Publish the product.

publish

Congratulations!, you have published your first API with API Connect.

Next Steps

You can test your APIs but heading over to Drafts > APIs > Assemble and clicking on the Play button next to the search bar.

play

Choose the get /api operation, and then click the Invoke button.

invoke

This response should be the same as the response as earlier in our how-to.

response

You can also navigate to the developer portal by going Dashboard > Sandbox > Settings > Portal to view the link to invite collaborators.

portal

Summary

With API connect you can connect various web services built in any language (such as Python) to your API Connect platform. With API connect, you can expose APIs, manage and track usage, all while ensuring your product is secure at the same time.

In this how-to, we just simply created one web service and binded it to API Connect. We covered how to:

  • Create a basic Python flask app with IBM Cloud
  • Create and publish APIs with API Connect
Aoun Lutfi