# API Management

# API Management Overview

API Management - the art of funnelling traffic from an API client through to the background service and taking care of things like authentication, rate-limiting, logging, auditing etc.

There are lots of solutions out there - both FOSS and commercial.

### Google Solutions

Google have three solutions that [they discuss on their cloud platform docs](https://cloud.google.com/blog/products/application-modernization/choosing-between-apigee-api-gateway-and-cloud-endpoints) site ([mirror](https://archive.jamesravey.me/archive/1669041790.953187/singlefile.html)):

- ApiGee - looks like the super expensive enterprisey option not really an option for new products
- API Gateway - seems to just provide a gateway that allows the internet to
- Cloud Gateway - seems to be a really customisable/complex version of API Gateway[![A flowchat describing the use cases in which you should use each product](https://wiki.jamesravey.me/uploads/images/gallery/2022-11/scaled-1680-/t5Ximage.png)](https://wiki.jamesravey.me/uploads/images/gallery/2022-11/t5Ximage.png)

Seems like API Gateway might be useful if you are running cloud functions. Apigee is SOO SOO expensive - it starts at $700/month.

### FOSS Solutions

Started exploring from [this list ](https://appinventiv.com/blog/open-source-api-management-tools/)([mirror](https://archive.jamesravey.me/archive/1669043105.483893/singlefile.html)) :

- Looked at API Umbrella - it seems like they haven't; made any releases since 2019 and I'm not sure how maintained it is so staying away for now.
- Started playing with Kong Gateway - it is lightweight, stores data in Postgres and looks easy to use with docker-compose.

# Kong Gateway

Kong Gateway is an open core project that offers many of the traditional [API Management](https://wiki.jamesravey.me/books/api-management/page/api-management-overview "API Management Overview") features.

### Minimal Docker Compose for Kong

```yaml
version: "3.0"

services:

  kong:
    env_file: .env
    image: kong/kong-gateway:3.0.1.0
    environment:
      - KONG_DATABASE=postgres
      - KONG_PG_HOST=db
      - KONG_PG_DATABASE=kong
      - KONG_PG_USER=postgres
      - KONG_PG_PASSWORD=${POSTGRES_PASSWORD}
      - KONG_ADMIN_LISTEN=0.0.0.0:8001
      - KONG_ADMIN_GUI_URL=http://localhost:8002
    ports:
      - 8000:8000 
      - 8443:8443 
      - 8001:8001 
      - 8444:8444 
      - 8002:8002 
      - 8445:8445 
      - 8003:8003 
      - 8004:8004 

  db:
    env_file: .env
    image: postgres
    restart: always
    volumes:
      - ./postgres:/var/lib/postgresql/data 
    ports:
      - 5432:5432
      
  adminer:
    env_file: .env
    image: adminer
    restart: always
    ports:
      - 8080:8080


```

And accompanying `.env` file:

```bash
POSTGRES_PASSWORD=example
KONG_PASSWORD="supersecret123"

```

### Docker Compose Installation

The official documentation for the project provides a walkthrough for docker installation [here](https://docs.konghq.com/gateway/3.0.x/install/docker/)

#### Seed the Database

`docker-compose run kong kong migrations bootstrap`

#### Starting Kong

`docker-compose up -d kong`

### Kong Config and Environment Variables

Kong normally reads a config file called `kong.conf` - an overview of valid configuration settings/directives can be found [here](https://docs.konghq.com/gateway/latest/reference/configuration/)

The docker image can read these variables from the environment. Simply copy the name of the thing, upercase it and prefix with `KONG_` e.g. `prefix` becomes `KONG_PREFIX`.