consul-node-sdk
v0.1.5
Published
Node.js SDK for Consul
Readme
Consul Node SDK
This is a Consul SDK for Node.js. The SDK provides a client for interacting with the Consul API.
Built with
- Language: TypeScript
- Bundler: tsdown
- Package manager: pnpm
Available Clients
agent: Consul agent APIcatalog: Consul catalog APIkv: Consul KV store APIhealth: Consul health APIsession: Consul session APIevent: Consul event APIstatus: Consul status APIcoordinate: Consul coordinate APIquery: Consul prepared query APItxn: Consul transaction APIsnapshot: Consul snapshot API
Usage
import { ConsulClient } from "./src/client.ts";
// Create a new client
const consul = new ConsulClient({
host: "localhost",
port: 8500,
secure: false,
token: "your-consul-token", // Optional
});
async function main() {
// Get the leader
const leader = await consul.status.leader();
console.log("Consul leader:", leader);
// Register a service
const serviceRegistered = await consul.agent.serviceRegister({
ID: "my-service-1",
Name: "my-service",
Tags: ["tag1", "tag2"],
Port: 8080,
});
console.log("Service registered:", serviceRegistered);
// Put a value in the KV store
const kvPut = await consul.kv.put("my-key", "my-value");
console.log("KV put result:", kvPut);
// Get a value from the KV store
const kvValue = await consul.kv.get("my-key");
console.log("KV value:", kvValue);
}