Skip to main content

Authentication and protected writes

API key and user session​

Every API call uses your backend's customer key (sk_…). Protected methods also take { session: token } with an ss_… user session. The SDK sends it in x-t3tris-session, alongside the bearer API key.

Keep both secrets on your server. Your application's browser session should use a secure, httpOnly cookie or an opaque identifier mapped to the SDK session on the backend. Do not put API keys or session tokens in URLs, analytics, logs or persistent browser storage.

A session proves identity. It does not grant contract permissions, attach vaults to a key or carry API scopes.

SIWE flow​

  1. Send the connected address and chain ID to your backend.
  2. Your backend calls auth.createSiweNonce with a configured application domain. Return only the generated message to the browser.
  3. Ask that same wallet to sign the exact message.
  4. Send the message and signature to your backend. Verify that the challenge belongs to the current browser session before calling auth.verifySiwe.
  5. Store the returned token on the backend and establish your application's cookie session.

Backend helpers:

import { createT3trisClient } from "@t3tris-labs/sdk";
import type { Address, Hex } from "viem";

const client = createT3trisClient({ apiKey: process.env.T3TRIS_API_KEY });

export function createChallenge(address: Address, chainId: number) {
// Use your deployed host, not an arbitrary domain supplied by the visitor.
return client.auth.createSiweNonce({ address, chainId, domain: "app.example.com" });
}

export function verifyChallenge(message: string, signature: Hex) {
// The route must first match message to this browser session's saved challenge.
return client.auth.verifySiwe({ message, signature });
}

In the browser, use your existing viem wallet client:

import type { WalletClient } from "viem";

export async function signChallenge(walletClient: WalletClient, message: string) {
if (!walletClient.account) throw new Error("Connect a wallet first.");
return walletClient.signMessage({ account: walletClient.account, message });
}

SIWE nonces are single-use and bound to the minted challenge. Sign the original message without reconstructing or editing it. Pass the connected chain ID when authenticating a contract wallet such as a Safe, because verification needs that chain's contract state. A read-only address cannot authenticate by itself.

Protected content and favorites​

Vault metadata and annotations require an authorized wallet session. The API checks the wallet's curator/owner or editor permissions. A valid login alone does not make the caller an editor.

import { createT3trisClient, type VaultRef } from "@t3tris-labs/sdk";

export async function updateVaultDescription(ref: VaultRef, sessionToken: string) {
const client = createT3trisClient({ apiKey: process.env.T3TRIS_API_KEY });
return client.vaults.updateMetadata(
ref,
{ description: "Our strategy, valuation process and risk disclosures." },
{ session: sessionToken },
);
}

accounts.setFavorite and accounts.deleteFavorite require a wallet session matching the account address. Account portfolio and activity reads use an address parameter without a session.

Contract actions use the wallet's on-chain permissions and do not use an API session. See vault operations.

Session lifecycle​

Use auth.whoami({ session }) to read the account and organization memberships. Treat 401 as a prompt to authenticate again, not as a reason to replay a signature.

import { createT3trisClient } from "@t3tris-labs/sdk";

export async function revokeSession(token: string, everywhere = false) {
const client = createT3trisClient({ apiKey: process.env.T3TRIS_API_KEY });
if (everywhere) await client.auth.logoutAll({ session: token });
else await client.auth.logout({ session: token });
// Also remove your own server session and expire the application cookie.
}

Logout revokes the presenting session. Logout-all revokes every session of that account, including sessions in other applications. Protect these routes against CSRF and require an explicit user action for logout-all.