SDK
March 10, 2026
The Second Layer SDK is a typed TypeScript client for the Second Layer API. It handles authentication, request formatting, and response parsing so you can focus on building.
Install with bun add @secondlayer/sdk.
Getting started
Create a client instance with your API key. The default base URL points to the hosted API.
import { SecondLayer } from "@secondlayer/sdk"
const client = new SecondLayer({
apiKey: "sk-sl_...",
baseUrl: "https://api.secondlayer.tools", // default
})
// Sub-clients are available as properties:
client.streams // stream CRUD, deliveries, replay
client.subgraphs // subgraph deploy, query, reindexStreams
Create and manage event streams. Streams deliver matching onchain events to your endpoint in real-time.
// Create — returns stream + signing secret for HMAC verification
const { stream, signingSecret } = await client.streams.create({
name: "my-stream",
endpointUrl: "https://example.com/streams",
filters: [
{ type: "stx_transfer", minAmount: 1_000_000 },
],
})
// List (optionally filter by status)
const { streams, total } = await client.streams.list({ status: "active" })
// Get by ID (supports partial IDs)
const stream = await client.streams.get("a1b2c3")
// Update filters or endpoint URL
await client.streams.update("a1b2c3", {
filters: [{ type: "contract_call", contractId: "SP1234...::token" }],
})
// Enable / disable / delete
await client.streams.enable("a1b2c3")
await client.streams.disable("a1b2c3")
await client.streams.delete("a1b2c3")
// Bulk operations
await client.streams.pauseAll()
await client.streams.resumeAll()
// Rotate signing secret
const { secret } = await client.streams.rotateSecret("a1b2c3")
// Inspect deliveries
const { deliveries } = await client.streams.listDeliveries("a1b2c3", {
limit: 20,
status: "failed",
})
const detail = await client.streams.getDelivery("a1b2c3", "delivery-id")Subgraphs
Deploy, query, and manage indexed subgraphs. The query API supports filtering with comparison operators, sorting, pagination, field selection, and full-text search.
// List deployed subgraphs
const { data } = await client.subgraphs.list()
// Get subgraph details (tables, health, row counts)
const subgraph = await client.subgraphs.get("token-transfers")
// Query a table
const { data, meta } = await client.subgraphs.queryTable(
"token-transfers",
"transfers",
{
sort: "_block_height",
order: "desc",
limit: 50,
filters: { sender: "SP1234...", "amount.gte": "1000000" },
fields: "sender,recipient,amount",
}
)
// meta = { total, limit, offset }
// Count rows
const { count } = await client.subgraphs.queryTableCount(
"token-transfers",
"transfers",
{ filters: { sender: "SP1234..." } }
)
// Reindex from scratch
await client.subgraphs.reindex("token-transfers", {
fromBlock: 150_000,
toBlock: 160_000,
})
// Delete subgraph and all data
await client.subgraphs.delete("token-transfers")Typed subgraphs
Import a subgraph definition to get a fully typed query client. Table names, column names, and filter operators are all inferred from your schema.
import { getSubgraph } from "@secondlayer/sdk"
import mySubgraph from "./subgraphs/token-transfers"
// Standalone helper — accepts options, SecondLayer instance, or Subgraphs instance
const client = getSubgraph(mySubgraph, { apiKey: "sk-sl_..." })
const rows = await client.transfers.findMany({
where: { sender: { eq: "SP1234..." }, amount: { gte: 1000000n } },
orderBy: { _blockHeight: "desc" },
limit: 25,
})
const total = await client.transfers.count({
sender: { eq: "SP1234..." },
})
// Or via the SecondLayer instance
const typed = client.subgraphs.typed(mySubgraph)
const rows = await typed.transfers.findMany({ ... })Error handling
All SDK methods throw ApiError on failure. The error includes the HTTP status code and a descriptive message.
import { ApiError } from "@secondlayer/sdk"
try {
await client.streams.get("nonexistent")
} catch (err) {
if (err instanceof ApiError) {
err.status // 404
err.message // "Stream not found"
}
}
// Common status codes:
// 401 — API key invalid or expired
// 404 — Resource not found
// 429 — Rate limited (check Retry-After header)
// 5xx — Server error