Skip to main content
Account and basin operations manage the lifecycle of your S2 resources: creating and configuring basins (which are namespaces, or large collections of streams), managing streams, and issuing access tokens. These map directly to the corresponding REST API endpoints, so the API reference is a particularly good companion to this page.

Basins

Basins are containers for streams, similar to buckets in object storage. Each basin belongs to an account and has its own configuration for default stream settings, which will be used when creating new streams unless explicitly overridden.
// List basins
const basins = await client.basins.list();

// Create a basin
await client.basins.create({ basin: "my-events" });

// Get configuration
const config = await client.basins.getConfig({ basin: "my-events" });

// Delete
await client.basins.delete({ basin: "my-events" });
See basin configuration for details on available options.

Streams

Streams live within basins and are the core data structure in S2. You can have an unlimited amount of streams, and create them instantly (even on first use). Data retention policies (e.g. TTL-based trimming), storage classes, timestamping behavior, are all configurable.
// List streams
const streams = await basin.streams.list({ prefix: "user-" });

// Create a stream
await basin.streams.create({
	stream: "user-actions",
	config: {
		/* optional configuration */
	},
});

// Get configuration
const streamConfig = await basin.streams.getConfig({ stream: "user-actions" });

// Delete
await basin.streams.delete({ stream: "user-actions" });
See stream configuration for more details on all options.

Access Tokens

Access tokens control what a client can do. Each token has a scope that restricts which basins and streams it can access, and what operations it can perform. See the access token API reference for the full specification, including operation groups, granular permissions, and auto-prefixing for multi-tenant setups.
// List tokens (returns metadata, not the secret)
const tokens = await client.accessTokens.list();

// Issue a token scoped to streams under "users/1234/"
const { accessToken: issuedToken } = await client.accessTokens.issue({
	id: "user-1234-rw-token",
	scope: {
		basins: { prefix: "" }, // all basins
		streams: { prefix: "users/1234/" },
		opGroups: { stream: { read: true, write: true } },
	},
	expiresAt: new Date("2027-01-01"),
});

// Revoke a token
await client.accessTokens.revoke({ id: "user-1234-rw-token" });

Metrics

Metrics are available at three levels: account, basin, and stream. Each returns time-series or scalar data for the specified metric set.
// Account-level: active basins over the last 30 days
const accountMetrics = await s2.metrics.account({
	set: "active-basins",
	start: new Date(Date.now() - 30 * 24 * 3600 * 1000),
	end: new Date(),
});

// Basin-level: storage usage with hourly resolution
const basinMetrics = await s2.metrics.basin({
	basin: "events",
	set: "storage",
	start: new Date(Date.now() - 6 * 3600 * 1000),
	end: new Date(),
	interval: "hour",
});

// Stream-level: storage for a specific stream
const streamMetrics = await s2.metrics.stream({
	basin: "events",
	stream: "user-actions",
	set: "storage",
	start: new Date(Date.now() - 3600 * 1000),
	end: new Date(),
	interval: "minute",
});
See the metrics API reference for available metric sets and response formats.