Quick Start Guide - API Proxy

This section is a step-by-step guide to create, publish, and invoke an API using the WSO2 API Manager (WSO2 API-M) Publisher and Developer Portal.

Before you begin...

  1. Install Oracle Java SE Development Kit (JDK) version 11 or 1.8 and set the JAVA_HOME environment variable. For more information on setting the JAVA_HOME environment variable for different operating systems, see Setup and Install.
  2. Download version 4.0.0 of WSO2 API-M.
  3. Start WSO2 API-M by navigating to the <API-M_HOME>/bin directory using the command-line and execute the following command api-manager.bat --run (for Windows) or sh api-manager.sh (for Linux).

What you'll build

  1. Creating and publishing an API via the Publisher Portal of WSO2 API-M.
  2. Subscribing to the API via the Developer Portal of WSO2 API-M and generating keys.
  3. Invoking the API with the generated keys.

Let's get started...

Step 1 - Create and publish an API

Follow the instructions below to create and publish an API via the Publisher Portal of WSO2 API-M.

  1. Navigate to the Publisher Portal https://localhost:9443/publisher and sign in with admin/admin as the credentials (The below home screen will be displayed).

    Publisher portal home page

  2. Create an API.

    Let's use a mock REST service to create a REST API from scratch.

    A mock service with a JSON response {"hello": "world"} is provided by default when you use the service URL as (http://run.mocky.io/v2/5185415ba171ea3a00704eed) that appears in the https://designer.mocky.io/ mock service. Note that we are using the HTTP protocol instead of HTTPS.

  3. Select REST API from the home screen and then click Start From Scratch.

    Design a new REST API

  4. Enter the API details.

    Name HelloWorld
    Context /hello
    Version 1.0.0
    Endpoint http://run.mocky.io/v2/5185415ba171ea3a00704eed

    Note

    Use the HTTP protocol because to use HTTPS you need to import the https://designer.mocky.io/ certificate into WSO2 API-M.

    Create an API

  5. Click Create & Publish.

    This will publish your first API on the Developer Portal as well as deploy it on the API Gateway. You now have an OAuth2.0 secured REST API that is ready to be consumed.

Step 2 - Subscribe to the API

Follow the instructions below to subscribe to the API and generate the keys via the Developer Portal of WSO2 API-M.

  1. Navigate to the Developer Portal.

    https://localhost:9443/devportal

    The published HelloWorld API is listed in the Developer Portal as shown below.

    Developer Portal home page

  2. Click Sign-In and enter admin/admin as your credentials to sign in to the Developer Portal.

  3. Click on the API thumbnail to view the overview of the API.

    API overview

  4. Register an OAuth2.0 application.

    1. Click Subscriptions on the left menu bar of the screen.

      Subscription Card

    2. Click SUBSCRIPTION & KEY GENERATION WIZARD in the above screen.

      This wizard walks you through 5 steps that will register an OAuth2.0 application which you will use to consume the HelloWorld API.

      Key generation wizard

    3. Create the OAuth2.0 application.

      Enter the application name, and click Next without changing any of the other default values.

      Application Name Greetings
      Per Token Quota 50PerMin

      Wizard - Create application

    4. Subscribe the application to the API.

      This subscribes the Greetings application to the HelloWorld API on the selected Business Plan. Click Next without changing any of the default values.

      Wizard - Subscribe to new application

    5. Generate the credentials for the Greetings OAuth2.0 application.

      The Grant Types define the various protocols, which will be allowed by the system, from which your application will be allowed to request tokens. Click Next.

      Wizard - Generate Keys

    6. Generate a test access token for the Greetings application to access the HelloWorld API.

      This step allows you to specify the permissions (scopes) for the token. Click Next without changing any of the default values.

      Wizard - Generate Access Token

    7. Click the copy icon, as shown below, to copy the generated test access token to the clipboard.

      Wizard - Copy Access Token

    8. Click Finish.

Voila!!! You can now test the HelloWorld API with the OAuth2.0 token that you just generated.

Step 3 - Invoke the API

Follow the instructions below to invoke the previously created API with the generated keys.

  1. Click Try Out on the left menu bar.

    The resources of the API will be listed.

  2. Paste the access token that you previously copied in the Access Token field.

    Paste the access token

  3. If this is the first time you are using the API test console from your browser, open a new tab and navigate to the https://localhost:8243/ URL.

    This will prompt your browser to accept the certificate used by the API Gateway. This is required because by default the API Gateway uses a self-signed certificate that is not trusted by web browsers.

    Note

    This certificate that is used by the API Gateway is replaced when deploying the system in production.

  4. Click on the GET resource of the API to expand the resource and Click Try It Out.

    GET resource

  5. Then click Execute.

    GET resource

    You should see the {"hello" : "world"} response from the API.

    Successful response

Congratulations! You have successfully created your first API, subscribed to it through an OAuth2.0 application, obtained an access token for testing, and invoked your API with the access token.

Automate API development and deployment

Let's look at how you can use WSO2 API Controller (apictl) which is the command-line tool to move APIs, API Products, and Applications across WSO2 API-M environments and to perform CI/CD operations. (For more information, see Getting Started with WSO2 API Controller (apictl))

Before you begin...

  1. Download the apictl version 4.0.0 (or the latest of the 4.x family) based your operating system from https://wso2.com/api-management/tooling/ from under the Dev-Ops Tooling section.

  2. Extract the ZIP to a preferred location.

    This location will be referred to as the apictl directory.

  3. To use the command line tool to navigate to the apictl directory.

    Warn

    If you have previously used an apictl old version, backup and remove the /home/<user>/.wso2apictl directory and reconfigure the environments using the commands as explained below.

    Execute the following command to view the available operations.

    ./apictl --help
  4. Point the apictl to the instance of WSO2 API-M in which you want to deploy APIs.

    Execute the following command to add an environment.

    Note

    It is assumed that WSO2 API-M is run locally (localhost) using the default ports.

    ./apictl add env dev \
             --apim https://localhost:9443

    For more information on adding environments using different flag combinations, see Add an environment.

    On successfully executing this command, you should see the following message.

    Default token endpoint 'https://localhost:9443/oauth2/token' is added as the token endpoint
    Successfully added environment 'dev'

Step 1 - Create an API

  1. Initialize an API project by providing a name for the project.

    Let's use the command below to create an API named PetstoreAPI. This creates a folder named PetstoreAPI in your current directory.

    ./apictl init PetstoreAPI --oas https://apim.docs.wso2.com/en/4.0.0/assets/attachments/get_started/petstore.json

    On successfully executing this command, you should see the following message.

    Initializing a new WSO2 API Manager project in <your-directory-path>/PetstoreAPI
    Project initialized
    Open README file to learn more

    Note

    Use the following command to view the various options related to initializing a project.

    ./apictl init --help
  2. Update the api.yaml file.

    1. Open and explore the PetstoreAPI folder with an IDE (e.g., VSCode).

    2. Open the api.yaml file. (Alternatively, You can use a text editor to open this file as well.)

    3. Change the values of the following attributes as shown below and save the file.

      lifeCycleStatus: PUBLISHED
      production_endpoints:
          url: https://petstore.swagger.io/v2
      sandbox_endpoints:
          url: https://petstore.swagger.io/v2

      Note

      • Make sure that there are no spaces in-between the context value in the api.yaml file.
      • Changing the default lifecycle status of the API from CREATED to PUBLISHED, will deploy the API directly to the Developer Portal and API Gateway, when you push this API to WSO2 API-M in the following step.
      • If you want to push this API to the Publisher Portal only, the status should be CREATED.

Step 2 - Publish the API

  1. Push the API to WSO2 API-M.

    Navigate back to the apictl directory and execute the following command:

    Info

    If you are working with a specific environment for the first time, you will be prompted to enter your account credentials on WSO2 API-M. You can use the default admin credentials as admin/admin.

    ./apictl import api --file ./PetstoreAPI --environment dev

    You should now see your API deployed successfully on WSO2 API-M.

  2. Browse the Publisher and the Developer Portals to view the API details.

Step 3 - Invoke the API

  1. Generate an access token using apictl.

    Navigate back to the apictl directory and execute the following command:

    ./apictl get keys -e dev -n SwaggerPetstore -v 1.0.0 -r admin

    You will get an access token that can be used to invoke your API. (For more information on generating keys using apictl, see Get keys for an API/API Product.)

  2. Invoke the API

    Execute the below cURL command to invoke the resource GET /pet of your API. (Make sure to enter the access token that you recieved earlier as the Bearer in thee request)

    curl -H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUz....RWrACAUNSFBpxz1lRLqFlDiaVJAg" https://localhost:8243/SwaggerPetstore/1.0.0/pet/5 -k

    You will receive the below response.

    {"id":5,"category":{"id":5,"name":"jimmy"},"name":"jimmy","photoUrls":[],"tags":[{"id":5,"name":"jimmy"}],"status":"pending"}

    Note

    Alternatively, you can consume the API as explained in the following sections using the WSO2 API-M Developer Portal.

    PetStore response

Top