@routr/sdk to create, read, update, and delete Connect resources from Node.js code. It is the same library rctl uses, and it talks to the API Server over gRPC on port 51908.
Prerequisites
- Node.js and npm.
- A running Routr server with a database-backed API Server, such as routr-one or the Helm chart.
- An existing Domain and Credentials for the Agent you create. You can create them with the SDK or with rctl.
Create an Agent
1
Install the SDK
Create a project and add the SDK as a dependency.
2
Configure a client
The SDK exposes one client class per resource: For a server with TLS and a self-signed certificate, pass the CA instead of
Acls, Agents, Credentials, Domains, Numbers, Peers, and Trunks. Each constructor takes the same options object.Setting the environment variable
ALLOW_INSECURE=true has the same effect as insecure: true.The following snippet creates an Agents client for a local server running without TLS.index.js
insecure.The package’s main export is a default export. In TypeScript or ES modules, use
import SDK from "@routr/sdk". In CommonJS, read .default as shown.3
Create the Agent
Add the following code to
index.js. It creates an Agent in an existing Domain with existing Credentials, then prints the stored resource. Replace the refs with your own.index.js
name and username are required. privacy accepts NONE or ID, and maxContacts: -1 means no limit. Use extended for your own metadata, such as an ID from another system.4
Run it
Run the script.
The script prints the new Agent’s
ref. rctl agents get --insecure lists the Agent.Read, update, and delete
Every client has the same five methods, named after its resource. For Agents they arecreateAgent, getAgent, listAgents, updateAgent, and deleteAgent. The following snippet lists Agents one page at a time, renames one, and deletes it.
updateAgent takes the ref plus only the fields to change. An Agent’s username can’t be updated.
Handle errors
Failed calls reject with a gRPC error. Checkcode against the gRPC status codes from @grpc/grpc-js.
Other languages
The SDK wraps the API Server’s gRPC interface. To use another language, generate a client from the protobuf files inmods/common/src/connect/protos.
Related
SDK reference
Every client, method, and request field.
Connect resources
What each resource represents.