Tutorial: Securing an API with a client ID and client secret

This tutorial shows you how to secure an API so that a calling application must supply a client ID and a client secret in IBM® API Connect Version 5.0.7 and later. This option is similar to requiring a user ID and password to be supplied.

Before you begin

Note: For tutorials about working with the developer toolkit in IBM API Connect Version 5.0.6 and earlier, see Developer toolkit tutorials for V5.0.6 and earlier.
The following diagram shows the sequential flow through the IBM API Connect Developer toolkit tutorials for working with API definitions that call an existing endpoint. Before beginning a tutorial, ensure that you have completed the previous tutorials in the sequence. You can click a tutorial in the diagram to open the instructions for that tutorial.
Tutorial flow diagram for API definitions that call an existing endpoint Installing the toolkit Prerequisites Tutorial: Creating and publishing an API definition from the command line Tutorial: Creating an invoke REST API definition Using the Developer Portal Tutorial: Securing an API with a client ID and client secret Tutorial: Securing APIs by using an LDAP user registry Tutorial: Securing an API by using OAuth 2.0

About this tutorial

You will modify the security settings for the Branches API, which you created in the tutorial Tutorial: Creating an invoke REST API definition, so that a calling application must supply a client ID and a client secret, then you will attempt to call the Branches API with and without the client ID and client secret, to verify that the client ID and client secret are required.

Setting the identification mechanism of an API

To modify the security settings for the Branches API so that a calling application must supply a client ID and client secret, complete the following steps:
  1. Change directories to your LoopBack® project and enter the following command:
    apic edit
    After a brief pause, the console displays this message:
    Express server listening on http://127.0.0.1:9000

    API Designer opens in your web browser, initially displaying the login page if you haven't logged in recently.

    Note: The login page prompts you to Sign in with IBM Cloud. Enter your IBM Cloud credentials, which authenticates you on IBM Cloud and provides access to the API Manager features such as Publish, Explore, and Analytics. You will continue to work in API Designer locally to create APIs, models and data sources.
    Note: If you need to run the editor on a different port, use the following command:
    [Icon indicating that this applies on Linux platforms][Icon indicating that this applies on Mac OS X platforms]
    PORT=port_number apic edit
    [Icon indicating that this applies on Windows platforms]
    set PORT=port_number && apic edit
    where port_number is the port number to use.
  2. Click APIs, then click the Branches REST API that you created in the tutorial Tutorial: Creating an invoke REST API definition.
  3. Navigate to the Security Definitions section.
  4. Note that, by default, a Client ID security definition already exists for your API.
  5. Click the Add Security Definition icon The add security definition icon. in the Security Definitions section, then select API Key. A new API Key security definition displays in the Security Definitions section.
  6. Change the value of the Name field to Client secret.
  7. Leave the value of the Parameter Name field as X-IBM-Client-Secret. You have defined a new security scheme.
    The Security Definitions pane showing the new Client secret security definition
  8. Navigate to the Security section.
  9. In the Security section, select Client secret (API Key), in addition to Client ID (API Key) which should already be selected by default.
    The Security section showing the Client ID and Client secret security definitions selected
  10. Click the Save icon The Save icon. to save your changes.

You have modified the operation so that a calling application must supply a client ID and client secret.

Calling an API by using a client ID and client secret

Now that you have determined the client ID and client secret for the Baggage Tracker application, you can supply them when calling the BankA API. For the purposes of this tutorial, you call the Branches API by testing it in the API Designer Explorer.

To call the Branches API by using a client ID and client secret, complete the following steps:
  1. Start the local test servers by completing the following steps:
    1. In the test console at the bottom of the screen, click the Start the servers icon:
      Start the servers icon
    2. Wait until the Running message is displayed:
      Running

      Depending on your project configuration and whether other processes are running, a different port number might be displayed.

    Note: If your Micro Gateway is already running, you must restart it before you can test your changes, by clicking the Restart the servers icon
    Restart the servers icon
    .
  2. Click Explore, then click Try it.
  3. Scroll down to the Identification section in the pane on the right.
    The Client ID field contains the value default, and the Client secret field contains the value SECRET in redacted form; these are the default values that are used for testing in the API Designer Explorer.
    The Identification section of the Explorer, showing the default client ID and client secret values

    When the API is published and becomes available to application developers through the Developer Portal, the API will be called by using application specific client ID and client secret values; for more information, see Adding an application.

  4. Remove the client ID and client secret values and click Call operation to test the API. The call fails.
  5. Restore the client ID and client secret value by entering default in the Client ID field and SECRET in the Client secret field, and click Call operation to test the API. The Branches response is returned correctly:
    Screen capture showing correct response from BankA API call

What you did in this tutorial

In this tutorial, you completed the following activities:
  • Set the identification mechanism of an API.
  • Called an API by using a client ID and client secret.