Skip to content

troposhq/kong-admin-api-client

Repository files navigation

Kong Admin API Node.js Library

The Kong Admin API Node library provides convenient access to the Kong Admin API from applications written in server-side JavaScript.

Installation

Install the package with:

npm install @tropos/kong-admin-api-client --save

Usage

The package needs to be configured with your Admin API's URL.

const Kong = require('@tropos/kong-admin-api-client');

const adminAPIURL = 'http://localhost:8001';
// Create a new client with the default exported constructor
const kong = new Kong({ adminAPIURL });

The kong object has properties that correspond to all the Kong Admin API resources. The currently supported resources are:

  • Services
  • Routes
  • Consumers
  • Credentials
// use these objects to interface with the API
kong.services
kong.routes
kong.consumers

All of the resources have standard methods you can use to access the API:

  • create
  • get
  • list
  • update
  • delete
// use the resource properties to interface with the API
// all the resources have the standard API methods
kong.services.get();
kong.routes.list();
kong.consumers.delete();

API

Services

#create

Creates a new service

Params

Attributes Description
name
optional
The Service name.
protocol The protocol used to communicate with the upstream. It can be one of http (default) or https.
host The host of the upstream server.
port The upstream server port. Defaults to 80.
path
optional
The path to be used in requests to the upstream server. Empty by default.
retries
optional
The number of retries to execute upon failure to proxy. The default is 5.
connect_timeout
optional
The timeout in milliseconds for establishing a connection to the upstream server. Defaults to 60000.
write_timeout
optional
The timeout in milliseconds between two successive write operations for transmitting a request to the upstream server. Defaults to 60000.
read_timeout
optional
The timeout in milliseconds between two successive read operations for transmitting a request to the upstream server. Defaults to 60000.
url
shorthand-attribute
Shorthand attribute to set protocol, host, port and path at once. This attribute is write-only (the Admin API never "returns" the url).

Example

await kong.services.create({
  name: 'my_service',
  url: 'https://jsonplaceholder.typicode.com/posts/1',
});

Returns

This method returns the direct response from the Kong Admin API server.

{
    "id": "4e13f54a-bbf1-47a8-8777-255fed7116f2",
    "created_at": 1488869076800,
    "updated_at": 1488869076800,
    "connect_timeout": 60000,
    "protocol": "http",
    "host": "example.org",
    "port": 80,
    "path": "/api",
    "name": "example-service",
    "retries": 5,
    "read_timeout": 60000,
    "write_timeout": 60000
}