Javascript API Client

The API Client package provides a Middleware API Client to use with the Scramjet Platform. It allows access to platform resources using an access token and helps with HostClient, SequenceClient and InstanceClient creation.


import { MiddlewareClient } from "@scramjet/middleware-api-client"; let middlewareClient = new MiddlewareClient(middlewareUrl);


The Platform requires requests to be authorized. Access token can be easily obtained by following steps from the quick start section. Request can be authorized by adding the Authorization header. The example below shows how to do it with the middleware-api-client.

import { MiddlewareClient } from "@scramjet/middleware-api-client"; import { ClientUtils } from "@scramjet/client-utils"; let middlewareClient = new MiddlewareClient(middlewareUrl); ClientUtils.setDefaultHeaders({ Authorization: `Bearer ${token}`, });

Users Space and HostClient

The Middleware API Client helps retrieve the list of spaces bound to the user. It has a helpful method that does just that. It's also the first step for the HostClient creation.

const spaces = await middlewareClient.getManagers(); // [ // { // "id":"org-11111111-2222-3333-4444-555555555555-manager" // } // ]

There is usually one Space available.

Next, we have to create the HostClient. For that to be possible the Space Manager has to be requested for the list of available hosts.

const spaceClient = middlewareClient.getManagerClient(spaces[0].id); const spaceHubs = await spaceClient.getHosts(); // [ // { // "id":"sth-0", // "info":{ // "created":"2022-07-07T13:37:30.416Z", // "lastConnected":"2022-07-14T07:39:34.781Z", // "lastDisconnected":"2022-07-14T07:38:41.105Z" // }, // "healthy":true, // "isConnectionActive":true // } // ]

There is usually one Hub available.

Having Hubs avaiable allows for the HostClient to be created.

const hostClient = spaceClient.getHostClient(spaceHubs[0].id);

HostClient allows for listing available Instances and Sequences and for the creation of corresponding clients.

Sequence and Instance clients

For the Sequence or Instance client to be created the entity id is needed. It can be retrived from the HostClient listing the instances and sequences. Creating HostClient in the context of Scramjet Platform was shown in the Users Space and HostClient section.

Creating SequenceClient

The SequenceClient requires the sequence id. It can be found in the list of sequences.

const sequencesList = await hostClient.listSequences(); // [ // { // "id":"11111111-2222-3333-4444-555555555555", // "config":{ // "type":"kubernetes", // "entrypointPath":"hello.js", // "version":"0.19.0", // "name":"@scramjet/hello-js", // "id":"11111111-2222-3333-4444-555555555555", // "sequenceDir":"/root/.scramjet_k8s_sequences/11111111-2222-3333-4444-555555555555", // "engines":{} // }, // "instances":[] // }, // (...) // ]

Having the Sequence id allows for the SequenceClient to be created and its methods to be called. For example start of the Sequence results in the creation of the new Instance.

const sequenceClient = hostClient.getSequenceClient(sequencesList[0].id); const instanceClient = await sequenceClient.start({ appConfig: {} });

Creating InstanceClient

The InstanceClient requires the instance id. It can be found in the list of instances.

const instancesList = await hostClient.listInstances(); // [ // { // "id":"21111111-3222-4333-5444-655555555555", // "appConfig":{}, // "sequence":"11111111-2222-3333-4444-555555555555", // "created":"2022-07-14T08:09:34.221Z", // "status":"starting" // }, // (...) // ]

Having the Instance id allows for the InstanceClient to be created and its methods to be called. For example, killing the Instance results in Instance immediate stop.

const instanceClient = hostClient.getInstanceClient(instancesList[0].id); await instanceClient.kill();


See the code documentation here:

More reading