Site iconAxway Blog

Amplify integration: how to modify API service description on discovery

In this blog post we’ll describe the steps required to modify an API Service description on discovery, using Amplify Webhooks and platform APIs using API Builder.

When APIs are discovered by the Amplify Central discovery agents, they appear in the Catalog -> Service Registry:

API Providers can view details about the discovered API by clicking on the API Service:

What if you want to display more information about the discovered API on this page? For example, what if you want to display information from the API’s OpenAPI Specification (OAS), such as the API designer’s contact info, on this page?

Amplify Integration Webhooks and Platform APIs can be used to modify the API Service description upon discovery. We covered this idea for a different use case in a prior blog post.

Let’s look at the steps required to modify an API Service description on discovery using Amplify Webhooks and platform APIs using API Builder. We’ll extract the API designers contact name and email address and prefix it to the existing description as follows:

The steps are as follows:

The API Builder project can be downloaded from Github.

A Docker Container can be downloaded from Docker Hub.

It’s worth noting that you could use any application or integration framework besides API Builder to do this as long as it can expose a webhook URL and make API calls.

Let’s get started!

Amplify integration for APIServiceRevision

We’ll trigger our webhook on APIServiceRevision creation events and not APIService creation events since the APIServiceRevision contains the OAS document in the webhook payload (and we don’t know if the API Service Revision is created when responding to the APIService event).

A sample YAML file for this is shown below. You need to edit the URL (and any required headers) for your webhook handler application.

api_service_revision_created_integration.yaml

name: api-service-revision-created-integration
kind: Integration
apiVersion: v1alpha1
title: API Service Revision Created Integration
tags:
- cloud
spec:
    description: This is an Integration for when an API Service Revision is created
---
name: api-service-revision-created-integration-wh
kind: Webhook
apiVersion: v1alpha1
title: API Service Created Webhook to invoke an API Builder API
metadata:
  scope:
    kind: Integration
    name: api-service-revision-created-integration
spec:
    enabled: true
    url: <YOUR WEBHOOK HANDLER APPLICATION URL>
    headers:
        Content-Type: application/json
---
group: management
apiVersion: v1alpha1
kind: ResourceHook
name: api-service-revision-created-integration-rh
title: Resource Hook to monitor all environments for new API Service Revision created
metadata:
  scope:
    kind: Integration
    name: api-service-revision-created-integration
spec:
  triggers:
    - group: management
      kind: APIServiceRevision
      name: '*'
      type:
        - created
      scope:
        kind: Environment
        name: '*'
  webhooks:
    - api-service-revision-created-integration-wh

You can use the YAML file to create the Integration (and associated webhook and resource hook) as follows:

axway central create -f api_service_revision_created_integration.yaml

If you modify your YAML, you can apply your changes as follows:

axway central apply -f api_service_revision_created_integration.yaml

Now whenever an API is discovered, a webhook call will be made to your application.

API Builder Webhook Handler Application

The API Builder project exposes one API:

POST /api/amplifycentralwebhookhandler

Once triggered the API parses the webhook payload and checks the Swagger/OAS doc and if it’s OAS3 modifies the description to add contact details.

The POST /api/amplifycentralwebhookhandler API flow is shown below:

In order to use the API Builder project or Docker Image, you need to set the following environment variables:

PORT=8080
CLIENT_ID=<AMPLIFY SERVICE ACCOUNT CLIENT ID>
CLIENT_SECRET=<AMPLIFY SERVICE ACCOUNT CLIENT SECRET>
API_KEY=<AN ARBITRARY API KEY FOR YOUR API BUILDER API THAT YOU CREATE AND USE IN YOUR INTEGRATION>
API_CENTRAL_URL=<YOUR AMPLIFY CENTRAL BASE URL>

A service account is required to make Amplify API calls (to GET and PUT the API Service). You can read about Amplify Service Accounts here.

Here are some details from the API Builder flow:

let oasB64Decoded = JSON.parse(Buffer.from(data.request.body.payload.spec.definition.value, 'base64').toString('utf8'));
return oasB64Decoded;

Summary

The Amplify platform is open and extensible thanks to its powerful webhooks and platform APIs. In this example, we saw how to add useful information from the API’s OAS3 document to the discovered API Service description.

Exit mobile version