mod.ts

TP-Link Router API client library for Deno.

Examples

Basic authentication and command execution

import { ACT, authenticate, execute } from "@hertzg/tplink-api";

const auth = await authenticate("http://192.168.1.1", {
  password: "admin",
});

if (auth) {
  const result = await execute(
    "http://192.168.1.1",
    [[ACT.GET, "LTE_BANDINFO"]],
    auth,
  );
  // result.error === 0 indicates success
  // result.actions[0].res contains the response data
}

Get unread SMS count with specific attributes

import { ACT, type Action } from "@hertzg/tplink-api";

const actions: Action[] = [
  [ACT.GET, "LTE_SMS_UNREADMSGBOX", ["totalNumber"]],
];

Retrieve SMS messages with pagination

import { ACT, type Action } from "@hertzg/tplink-api";

const actions: Action[] = [
  [ACT.SET, "LTE_SMS_UNREADMSGBOX", { pageNumber: "1" }],
  [ACT.GS, "LTE_SMS_UNREADMSGENTRY", ["index", "from", "content", "receivedTime"]],
];

Functions

Interfaces

I
ActionResult

Result of a single action execution, mapping request to response.

I
AuthOptions

Options for authenticating with the router.

I
AuthResult

Result of successful authentication containing all data needed for API calls.

I
BusyStatus

Login and busy state reported by the router before authentication.

I
CommandBatch

One HTTP round trip's worth of actions.

I
Credentials

Credentials as supplied by the caller, after the dialect's username default has been applied.

I
DecodedBatch

Decoded result of one round trip, positionally aligned with CommandBatch.indices.

I
Dialect

A firmware family's wire protocol, expressed as pure functions and data.

I
Envelope

Encrypted request envelope: AES ciphertext plus the RSA-encrypted signature.

  • data: string

    Base64 AES-CBC ciphertext of the plaintext payload.

  • sign: string

    Hex RSA-encrypted parameter string: key&iv&h&s for login, h&s for commands.

I
ExecuteOptions

Options required for executing commands on the router. These values are obtained from authenticate.

I
ExecuteResult

Result of executing one or more actions on the router.

I
PublicKeyInfo

RSA public key material and the request sequence base.

I
RouterInfo

Variables scraped from the router's login page.

I
SessionContext

Session material carried on every request made after login.

Type Aliases

T
T
ActionType = (ACT)[keyof ACT]

Numeric action type value from the ACT constant.

Variables

v
gdprJson: Dialect

EU/GDPR firmware speaking a JSON payload format over /cgi_gdpr?9.

v
gdprText: Dialect

EU/GDPR firmware speaking the bespoke text payload format over /cgi_gdpr.

dialect/mod.ts

Firmware dialects for the TP-Link router API.

Examples

Select a dialect for a router that speaks JSON

import { assertEquals } from "@std/assert";
import { gdprJson, gdprText } from "./mod.ts";

assertEquals(gdprText.defaultUsername, "admin");
assertEquals(gdprJson.defaultUsername, "user");

Derive a dialect for a model that differs in one place

import { assertEquals } from "@std/assert";
import { type Dialect, gdprJson } from "./mod.ts";

const vx800v: Dialect = {
  ...gdprJson,
  id: "vx800v",
  publicKeyRequest: (baseUrl) =>
    new Request(new URL("cgi/getParm", baseUrl), { method: "POST" }),
};

assertEquals(
  vx800v.publicKeyRequest("http://192.168.1.1").url,
  "http://192.168.1.1/cgi/getParm",
);
assertEquals(vx800v.defaultUsername, "user");

Interfaces

I
BusyStatus

Login and busy state reported by the router before authentication.

I
CommandBatch

One HTTP round trip's worth of actions.

I
Credentials

Credentials as supplied by the caller, after the dialect's username default has been applied.

I
DecodedBatch

Decoded result of one round trip, positionally aligned with CommandBatch.indices.

I
Dialect

A firmware family's wire protocol, expressed as pure functions and data.

I
Envelope

Encrypted request envelope: AES ciphertext plus the RSA-encrypted signature.

  • data: string

    Base64 AES-CBC ciphertext of the plaintext payload.

  • sign: string

    Hex RSA-encrypted parameter string: key&iv&h&s for login, h&s for commands.

I
PublicKeyInfo

RSA public key material and the request sequence base.

I
RouterInfo

Variables scraped from the router's login page.

I
SessionContext

Session material carried on every request made after login.

Type Aliases

T
T
ActionType = (ACT)[keyof ACT]

Numeric action type value from the ACT constant.

Variables

v
gdprJson: Dialect

EU/GDPR firmware speaking a JSON payload format over /cgi_gdpr?9.

v
gdprText: Dialect

EU/GDPR firmware speaking the bespoke text payload format over /cgi_gdpr.