Automate your API catalog with Dev Portal

Uses: Kong Gateway Dev Portal decK
TL;DR

You can automate the creation and publication of APIs to your Dev Portal API catalog using the Konnect API. Create an API (/v3/apis), optionally associate a document (/v3/apis/{apiId}/documents) or spec (/v3/apis/{apiId}/versions) with the API, then associate the API with a Gateway Service (/v3/apis/{apiId}/implementations). Finally, publish it by sending a PUT request to the /v3/apis/{apiId}/publications/{portalId} endpoint.

Prerequisites

This is a Konnect tutorial and requires a Konnect personal access token.

  1. Create a new personal access token by opening the Konnect PAT page and selecting Generate Token.

  2. Export your token to an environment variable:

     export KONNECT_TOKEN='YOUR_KONNECT_PAT'
    
  3. Run the quickstart script to automatically provision a Control Plane and Data Plane, and configure your environment:

     curl -Ls https://get.konghq.com/quickstart | bash -s -- -k $KONNECT_TOKEN --deck-output
    

    This sets up a Konnect Control Plane named quickstart, provisions a local Data Plane, and prints out the following environment variable exports:

     export DECK_KONNECT_TOKEN=$KONNECT_TOKEN
     export DECK_KONNECT_CONTROL_PLANE_NAME=quickstart
     export KONNECT_CONTROL_PLANE_URL=https://us.api.konghq.com
     export KONNECT_PROXY_URL='http://localhost:8000'
    

    Copy and paste these into your terminal to configure your session.

This tutorial requires Kong Gateway Enterprise. If you don’t have Kong Gateway set up yet, you can use the quickstart script with an enterprise license to get an instance of Kong Gateway running almost instantly.

  1. Export your license to an environment variable:

     export KONG_LICENSE_DATA='LICENSE-CONTENTS-GO-HERE'
    
  2. Run the quickstart script:

     curl -Ls https://get.konghq.com/quickstart | bash -s -- -e KONG_LICENSE_DATA 
    

    Once Kong Gateway is ready, you will see the following message:

     Kong Gateway Ready
    

decK is a CLI tool for managing Kong Gateway declaratively with state files. To complete this tutorial you will first need to install decK.

For this tutorial, you’ll need Kong Gateway entities, like Gateway Services and Routes, pre-configured. These entities are essential for Kong Gateway to function but installing them isn’t the focus of this guide. Follow these steps to pre-configure them:

  1. Run the following command:

    echo '
    _format_version: "3.0"
    services:
      - name: example-service
        url: http://httpbin.konghq.com/anything
    routes:
      - name: example-route
        paths:
        - "/anything"
        service:
          name: example-service
    ' | deck gateway apply -
    

To learn more about entities, you can read our entities documentation.

For this tutorial, you’ll need a Dev Portal pre-configured. These settings are essential for Dev Portal to function, but configuring them isn’t the focus of this guide. If you don’t have these settings already configured, follow these steps to pre-configure them:

  1. Create a Dev Portal:

    curl -X POST "$KONNECT_CONTROL_PLANE_URL/v3/portals" \
         -H "Accept: application/json"\
         -H "Content-Type: application/json"\
         -H "Authorization: Bearer $DECK_KONNECT_TOKEN" \
         --json '{
           "name": "MyDevPortal",
           "authentication_enabled": false,
           "auto_approve_applications": false,
           "auto_approve_developers": false,
           "default_api_visibility": "public",
           "default_page_visibility": "public"
         }'
    
  2. Export your Dev Portal ID and URL from the output:
    export PORTAL_ID='YOUR-DEV-PORTAL-ID'
    export PORTAL_URL='YOUR-DEV-PORTAL-DOMAIN'
    
  3. Create a page in your Dev Portal so published APIs will display:

    curl -X POST "$KONNECT_CONTROL_PLANE_URL/v3/portals/$PORTAL_ID/pages" \
         -H "Accept: application/json"\
         -H "Content-Type: application/json"\
         -H "Authorization: Bearer $DECK_KONNECT_TOKEN" \
         --json '{
           "title": "My Page",
           "slug": "/apis",
           "description": "A custom page about developer portals",
           "visibility": "public",
           "status": "published",
           "content": "# Welcome to My Dev Portal\nExplore the available APIs below:\n::apis-list\n---\npersist-page-number: true\ncta-text: \"View APIs\"\n---\n"
         }'
    

Create an API

In this tutorial, you’ll automate your API catalog by creating an API along with a document and spec, associating it with a Gateway Service, and finally publishing it to a Dev Portal.

First, create an API using the /v3/apis endpoint:

curl -X POST "$KONNECT_CONTROL_PLANE_URL/v3/apis" \
     -H "Accept: application/json"\
     -H "Content-Type: application/json"\
     -H "Authorization: Bearer $DECK_KONNECT_TOKEN" \
     --json '{
       "name": "MyAPI",
       "attributes": {
         "env": [
           "development"
         ],
         "domains": [
           "web",
           "mobile"
         ]
       }
     }'

Export the ID of your API from the response:

export API_ID='YOUR-API-ID'

Create and associate an API spec and version

Create and associate a spec and version with your API using the /v3/apis/{apiId}/versions endpoint:

curl -X POST "$KONNECT_CONTROL_PLANE_URL/v3/apis/$API_ID/versions" \
     -H "Accept: application/json"\
     -H "Content-Type: application/json"\
     -H "Authorization: Bearer $DECK_KONNECT_TOKEN" \
     --json '{
       "version": "1.0.0",
       "spec": {
         "content": "{\"openapi\":\"3.0.3\",\"info\":{\"title\":\"Example API\",\"version\":\"1.0.0\"},\"paths\":{\"/example\":{\"get\":{\"summary\":\"Example endpoint\",\"responses\":{\"200\":{\"description\":\"Successful response\"}}}}}}"
       }
     }'

APIs should have API documents or specs, and can have both. If neither are specified, Konnect can’t render documentation.

Create and associate an API document

An API document is Markdown documentation for your API that displays in the Dev Portal. You can link multiple API documents to each other with a parent document and child documents.

Create and associate an API document using the /v3/apis/{apiId}/documents endpoint:

curl -X POST "$KONNECT_CONTROL_PLANE_URL/v3/apis/$API_ID/documents" \
     -H "Accept: application/json"\
     -H "Content-Type: application/json"\
     -H "Authorization: Bearer $DECK_KONNECT_TOKEN" \
     --json '{
       "slug": "api-document",
       "status": "published",
       "title": "API Document",
       "content": "# API Document Header"
     }'

Associate the API with a Gateway Service

Gateway Services represent the upstream services in your system. By associating a Service with an API, this allows developers to generate credentials or API keys for your API.

Before you can associate the API with the Service, you need the Control Plane ID and the ID of the example-service Service you created in the prerequisites.

First, send a request to the /v2/control-planes endpoint to get the ID of the quickstart Control Plane:

curl -X GET "$KONNECT_CONTROL_PLANE_URL/v2/control-planes?filter%5Bname%5D%5Bcontains%5D=quickstart" \
     -H "Accept: application/json"\
     -H "Content-Type: application/json"\
     -H "Authorization: Bearer $DECK_KONNECT_TOKEN"

Export your Control Plane ID:

export CONTROL_PLANE_ID='YOUR-CONTROL-PLANE-ID'

Next, list Services by using the /v2/control-planes/{controlPlaneId}/core-entities/services endpoint:

curl -X GET "$KONNECT_CONTROL_PLANE_URL/v2/control-planes/$CONTROL_PLANE_ID/core-entities/services" \
     -H "Accept: application/json"\
     -H "Content-Type: application/json"\
     -H "Authorization: Bearer $DECK_KONNECT_TOKEN"

Export the ID of the example-service:

export SERVICE_ID='YOUR-GATEWAY-SERVICE-ID'

Associate the API with a Service using the /v3/apis/{apiId}/implementations endpoint:

curl -X POST "$KONNECT_CONTROL_PLANE_URL/v3/apis/$API_ID/implementations" \
     -H "Accept: application/json"\
     -H "Content-Type: application/json"\
     -H "Authorization: Bearer $DECK_KONNECT_TOKEN" \
     --json '{
       "service": {
         "control_plane_id": "'$CONTROL_PLANE_ID'",
         "id": "'$SERVICE_ID'"
       }
     }'

Publish the API to Dev Portal

Now you can publish the API to your Dev Portal using the /v3/apis/{apiId}/publications/{portalId} endpoint:

curl -X PUT "$KONNECT_CONTROL_PLANE_URL/v3/apis/$API_ID/publications/$PORTAL_ID" \
     -H "Accept: application/json"\
     -H "Content-Type: application/json"\
     -H "Authorization: Bearer $DECK_KONNECT_TOKEN"

Validate

To validate that the API was created and published in your Dev Portal, navigate to your Dev Portal:

open https://$PORTAL_URL/apis

You should see MyAPI in the list of APIs. If an API is published as private, you must enable Dev Portal RBAC and developers must sign in to see APIs.

Cleanup

If you created a new control plane and want to conserve your free trial credits or avoid unnecessary charges, delete the new control plane used in this tutorial.

Something wrong?

Help us make these docs great!

Kong Developer docs are open source. If you find these useful and want to make them better, contribute today!