fb
WSO2 API Manager 5 minutes

Working with samples with the WSO2 API Manager

Rob Blaauboer
Rob Blaauboer
Integration Consultant & WSO2 Trainer
WSO2 API Manager samples
Scroll

In general, all WSO2 products have a number of samples included. The WSO2 API Manager is no exception. However, it’s again different from the Enterprise Integrator, as well as the WSO2 Identity Server that we covered in previous blogs. When you download an out-of-the-box WSO2 API Manager and install it on your local machine or run a server, there is a sample API (war file) that you can deploy. This is, of course, not a full-blown full-scale API. It’s quite a limited API. But it does allow you to try out the product without having to bother finding an API back-end that you can use and doing all kinds of other stuff.

In this blog, I’ll cover some other APIs that are also available on the API Manager, as well as look at some of the samples that we know have been around.

APIs

So, let’s start with the APIs that are there. Strictly speaking, they are not APIs, as there are .WAR files available in the embedded Tomcat of the API Manager. 

For people who are familiar with older versions, these war files might seem familiar, because we had a few different sample APIs over the years. But how do you know where to go and how to configure them? Well, that is a little bit of a puzzle. But luckily, I’m here to do that for you. 

Using several resources online I was able to find a deployed Calculator API somewhere in a WSO2 API Store on the internet. From there I downloaded the swagger 2.0 (or OpenAPI 2.0) definition. This is the definition of the API but pertained to an older WSO2 API Manager version (2.6.0). 

I created an API in my API Manager 3.2.0 and imported the swagger definition into the WSO2 API Manager. This gave me the right resources and the only thing I had to figure out was the URL. That was easy because I could see it on the console.

[2021-07-02 09:30:30,087]  INFO - TomcatGenericWebappsDeployer Deployed webapp: StandardEngine[Catalina].StandardHost[localhost].StandardContext[/am/sample/calculator/v1].File[C:\Users\rob\Desktop\SAMLES~1\WSO2AM~1.0\bin\..\repository\deployment\server\webapps\am#sample#calculator#v1.war]
Samples API Manager 1

This is the swagger file, if you want to create the API on your WSO2 API Manager.

swagger: '2.0'
info:
  description: >-
    Simple calculator API to perform addition, subtraction, multiplication and
    division.
  version: '1.0'
  title: Calculator
host: localhost
basePath: /calc/1.0
schemes:
  - https
  - http
security:
  - default: []
paths:
  /add:
    get:
      summary: add x and y
      parameters:
        - name: x
          in: query
          required: true
          type: string
        - name: 'y'
          in: query
          required: true
          type: string
      responses:
        '200': {}
      security:
        - default: []
      x-auth-type: Application & Application User
      x-throttling-tier: Unlimited
      x-wso2-application-security:
        security-types:
          - oauth2
        optional: false
  /subtract:
    get:
      summary: subtract y from x
      parameters:
        - name: x
          in: query
          required: true
          type: string
        - name: 'y'
          in: query
          required: true
          type: string
      responses:
        '200': {}
      security:
        - default: []
      x-auth-type: Application & Application User
      x-throttling-tier: Unlimited
      x-wso2-application-security:
        security-types:
          - oauth2
        optional: false
  /multiply:
    get:
      summary: multiply x by y
      parameters:
        - name: x
          in: query
          required: true
          type: string
        - name: 'y'
          in: query
          required: true
          type: string
      responses:
        '200': {}
      security:
        - default: []
      x-auth-type: Application & Application User
      x-throttling-tier: Unlimited
      x-wso2-application-security:
        security-types:
          - oauth2
        optional: false
  /divide:
    get:
      summary: divide x by y
      parameters:
        - name: x
          in: query
          required: true
          type: string
        - name: 'y'
          in: query
          required: true
          type: string
      responses:
        '200': {}
      security:
        - default: []
      x-auth-type: Application & Application User
      x-throttling-tier: Unlimited
      x-wso2-application-security:
        security-types:
          - oauth2
        optional: false
securityDefinitions:
  default:
    type: oauth2
    authorizationUrl: 'https://test.com'
    flow: implicit
x-wso2-auth-header: Authorization
x-wso2-cors:
  corsConfigurationEnabled: false
  accessControlAllowOrigins:
    - '*'
  accessControlAllowCredentials: false
  accessControlAllowHeaders:
    - authorization
    - Access-Control-Allow-Origin
    - Content-Type
    - SOAPAction
    - apikey
    - testKey
  accessControlAllowMethods:
    - GET
    - PUT
    - POST
    - DELETE
    - PATCH
    - OPTIONS
x-wso2-production-endpoints:
  urls:
    - 'https://localhost:9443/am/sample/calculator/v1/api/'
  type: http
x-wso2-sandbox-endpoints:
  urls:
    - 'https://localhost:9443/am/sample/calculator/v1/api/'
  type: http
x-wso2-basePath: /calc/1.0
x-wso2-transports:
  - http
  - https
x-wso2-application-security:
  security-types:
    - oauth2
  optional: false
x-wso2-response-cache:
  enabled: false
  cacheTimeoutInSeconds: 300

Other samples

The second type of resources are, of course, the API samples that we can find on GitHub. And as you know, who uses GitHub for the repository of the products like WSO2 API Manager, WSO2 Enterprise Integrator and WSO2 Identity Server, but also, the samples are stored there. 

Unfortunately, not all the samples are up-to-date. When you’re looking in the documentation, you’ll see that there are some references to previous versions of the API manager like WSO2 API Manager 2.6.0, which is already a couple of years old. That doesn’t mean that it will not work on version 3.2.0 (the one I’m using now). But it’s always good to see if there is a newer version of a specific sample as it can use different functionalities that have changed between versions.

I am downloading the zip file (you can also use Git of course). The samples are partly Java programs that you need to compile. But there are also Docker compose files, Micro Integrator files with Kubernetes and streaming API back-ends. Compilation, or rather building, requires Maven and Java.

Samples API Manager 2

These are the sample directories. I am ignoring files that are not directories.

SampleExplanationInstall / Java mvn build
apim-tutorialAPI Manager 4.0.0 tutorial series, docker compose setupInstall
CustomGatewayJWTGeneratorAPI Gateway CustomJWT API Manager 3.1.0Build
CustomJWTGeneratorCustomJWTGenerator for API Manager 2.6.0 / 3.0.0Build
CustomJWTTransformerSecure APIs using JWT (Self Contained) Access Tokens for API Manager 3.1.0Build
graphql-backendSimple Graphql ServerInstall
kubernetes-demoKubernetes demoInstall
mi-cicdMicro Integrator CICD Docker & KubernetesInstall
streaming-api-backendsWebsocket backend and SSE backendBuild / Install
XACML-handlerXACML Entitlement handlerBuild

Conclusion

As you can see it is quite a combination of samples for multiple versions of API Manager, from 2.6.0 up to 4.0.0.

I must say that I have not yet looked into most of them, especially not the Docker and Kubernetes demos, but I will take an in depth look and perhaps, if it is a valuable sample, I will write a blog about it.

The Calculator API for which you now have the swagger file, can be used as another simple sample API on the API Manager and can be used for examples and demos. 

Learning more about the WSO2 API Manager

I hope you enjoyed reading this blog. If you are interested in learning more about the WSO2 API Manager, why don’t you have a look at the WSO2 training we offer. As of October 2021, we are starting with our instructor lead face to face trainings in The Netherlands, United States and incompany trainings at your office or a training venue nearby for groups of trainees from one organization.