Toxiproxy Node Client

4.0.0 · active · verified Sun Apr 19

The `toxiproxy-node-client` is a JavaScript and TypeScript client library for interacting with Toxiproxy, a powerful tool developed by Shopify for simulating real-world network conditions such as high latency, bandwidth limitations, and connection drops. This library, currently at stable version 4.0.0, provides a programmatic interface to configure Toxiproxy proxies and add various network 'toxics' to test the resilience and error handling of applications. While there isn't a fixed public release cadence, major versions like v4 typically introduce significant API updates or broader compatibility. Its key differentiator is being the primary Node.js interface for Toxiproxy, enabling developers to integrate network chaos engineering directly into their Node.js testing and development workflows without manual intervention. It ships with full TypeScript type definitions, making it suitable for both JavaScript and TypeScript projects.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a Toxiproxy client, set up a proxy for a Redis instance, add a bandwidth-limiting toxic, and then clean up both the toxic and the proxy. It includes error handling and guidance for ensuring the Toxiproxy server is running.

import { Toxiproxy, ICreateProxyBody, ICreateToxicBody, Bandwidth } from "toxiproxy-node-client";

async function run() {
    const toxiproxy = new Toxiproxy("http://localhost:8474");
    const proxyBody: ICreateProxyBody = {
      listen: "localhost:0", // Listen on an ephemeral port
      name: "redis-proxy", // Give it a unique name
      upstream: "redis:6379" // Target the actual Redis instance
    };

    try {
        console.log(`Creating proxy for upstream: ${proxyBody.upstream}`);
        const proxy = await toxiproxy.createProxy(proxyBody);
        console.log(`Proxy '${proxy.name}' created, listening on ${proxy.listen}`);

        const toxicBody: ICreateToxicBody<Bandwidth> = {
          attributes: { rate: 1000 }, // 1000 KB/s
          type: "bandwidth",
          name: "limit-bandwidth"
        };

        console.log(`Adding bandwidth toxic (${toxicBody.attributes.rate} KB/s) to proxy '${proxy.name}'`);
        const toxic = await proxy.addToxic(toxicBody);
        console.log(`Toxic '${toxic.name}' of type '${toxic.type}' added.`);

        // In a real scenario, you would now run your tests against proxy.listen
        // e.g., connect your Redis client to proxy.listen, not redis:6379 directly.

        // Clean up: remove the toxic and then the proxy
        console.log(`Removing toxic '${toxic.name}'...`);
        await proxy.deleteToxic(toxic.name);
        console.log(`Toxic removed. Deleting proxy '${proxy.name}'...`);
        await toxiproxy.deleteProxy(proxy.name);
        console.log("Proxy deleted successfully.");
    } catch (error: any) {
        console.error("Error interacting with Toxiproxy:", error.message);
        console.error("Ensure Toxiproxy server is running on http://localhost:8474");
        console.error("You can run it via Docker: `docker run --rm -p 8474:8474 ghcr.io/shopify/toxiproxy:latest`");
    }
}

run();

view raw JSON →