# Getting started

Visibling is product analytics, error tracking, and customer support observability for apps that run on Cloudflare. You can wire it end to end in about ten minutes: create a project, mount a first-party proxy on your own Worker, and initialize the browser client.

This quickstart uses TanStack Start. The [first-party proxy guide](/docs/proxy) has standalone Cloudflare Worker and same-account Service Binding variants.

## 1. Create a project and a server key

1. Sign in at [visibling.com](https://visibling.com/sign-in), or create a free workspace at [visibling.com/sign-up](https://visibling.com/sign-up).
2. Create a project for the app you are instrumenting, and note its organization slug and project slug.
3. Open **Project settings → Keys** and create a **server** key. It starts with `vis_srv_` and is shown once.
4. Store the key as a Worker secret instead of committing it:

```sh
wrangler secret put VISIBLING_SERVER_KEY
```

Visibling also issues `vis_pub_` browser keys and `vis_map_` source map keys. The server key is the one the first-party proxy uses to attach signed-in-user identity, so it must never be sent to the browser.

## 2. Install the SDK

```sh
pnpm add @visibling/sdk
```

`npm install @visibling/sdk` and `yarn add @visibling/sdk` work too. The package ships ESM-only subpath exports for the browser client, the server client, the Cloudflare proxy, the Tail Worker helper, and the one-file AI-agent/MCP middleware (`@visibling/sdk/agent`).

## 3. Mount the first-party proxy

The browser posts events to `/_v/e` on your own origin. A Worker route forwards that batch to Visibling with the server key and the user identity your app already resolved server-side.

TanStack Start route handlers do not receive the Worker `env` or `ctx`, so stash them in `AsyncLocalStorage` from the Worker entrypoint. `nodejs_compat` must be enabled:

```jsonc
// wrangler.jsonc
{
  "compatibility_flags": ["nodejs_compat"]
}
```

```ts
// src/lib/request-context.ts
import { AsyncLocalStorage } from "node:async_hooks";
import type { Env } from "./visibling-proxy";

interface RequestContext {
  env: Env;
  ctx: ExecutionContext;
}

const storage = new AsyncLocalStorage<RequestContext>();

export function setRequestContext<T>(
  env: Env,
  ctx: ExecutionContext,
  callback: () => T | Promise<T>,
): Promise<T> {
  return Promise.resolve(storage.run({ env, ctx }, callback));
}

export function getEnv(): Env {
  const context = storage.getStore();
  if (!context) throw new Error("Env is only available inside a request.");
  return context.env;
}

export function getExecutionContext(): ExecutionContext | undefined {
  return storage.getStore()?.ctx;
}
```

```ts
// src/lib/visibling-proxy.ts
import type { VisiblingIdentity } from "@visibling/sdk/cloudflare";
import { createVisiblingProxy } from "@visibling/sdk/cloudflare";
import { getEnv } from "./request-context";
import { getSession } from "./auth-session"; // your own session lookup

interface Env {
  VISIBLING_SERVER_KEY: string;
  APP_RELEASE?: string;
  APP_ENVIRONMENT?: string;
}

async function getIdentity(request: Request): Promise<VisiblingIdentity | null> {
  const session = await getSession(request);
  if (!session?.user) return null;
  return {
    distinctId: session.user.id,
    email: session.user.email,
    userName: session.user.name,
    externalOrgId: session.orgId,
    plan: session.plan,
  };
}

export function visiblingProxy() {
  const env = getEnv();
  return createVisiblingProxy({
    serverKey: env.VISIBLING_SERVER_KEY,
    getIdentity,
    release: env.APP_RELEASE,
    environment: env.APP_ENVIRONMENT,
  });
}
```

Register the route in the virtual route tree, then mount the handler:

```ts
// src/routes.ts
import { index, rootRoute, route } from "@tanstack/virtual-file-routes";

export const routes = rootRoute("__root.tsx", [
  index("index.tsx"),
  route("/_v/e", "_v/e.tsx"),
]);
```

```tsx
// src/routes/_v/e.tsx
import { createFileRoute } from "@tanstack/react-router";
import { getExecutionContext } from "../../lib/request-context";
import { visiblingProxy } from "../../lib/visibling-proxy";

export const Route = createFileRoute("/_v/e")({
  server: {
    handlers: {
      POST: ({ request }) => visiblingProxy()(request, getExecutionContext()),
    },
  },
});
```

Finally, wrap TanStack Start in the Worker entrypoint so every handler can reach the request context:

```ts
// src/server.ts
import { createStartHandler, defaultStreamHandler } from "@tanstack/react-start/server";
import { setRequestContext } from "./lib/request-context";
import type { Env } from "./lib/visibling-proxy";

const handler = createStartHandler(defaultStreamHandler);

export default {
  async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
    return setRequestContext(env, ctx, () => handler(request));
  },
};
```

Passing `ctx` lets the proxy answer `202 Accepted` immediately and forward in `ctx.waitUntil`. Without it, the helper awaits the forward before answering so no batch is silently dropped.

## 4. Initialize the browser client

```ts
// src/lib/visibling.ts — client code, imported from components, never from the Worker entry
import { createVisiblingClient } from "@visibling/sdk/browser";

export const visibling = createVisiblingClient({
  endpoint: "/_v",
  release: import.meta.env.VITE_RELEASE,
  environment: import.meta.env.VITE_ENVIRONMENT,
});
```

`endpoint: "/_v"` posts batches to `/_v/e`; the client appends `/e` once. Import the module from your root route so the client captures the first pageview, then use `visibling.track(...)` from components.

Do not call `visibling.identify()` to attach your signed-in user in proxy mode. The proxy forwards with a server key and discards browser-supplied `distinctId`, `email`, `userName`, `plan`, and `externalOrgId`; resolve the user in `getIdentity` instead. Anonymous traffic still stitches through the browser-owned `anonId`.

## 5. Track and verify

```ts
visibling.track("checkout_started", { plan: "starter" });
```

Send a smoke batch straight to the mounted route:

```sh
curl -i -X POST http://localhost:8787/_v/e \
  -H 'content-type: application/json' \
  -d '{"items":[{"type":"track","name":"proxy_smoke"}]}'
```

A healthy proxy answers `HTTP/1.1 202 Accepted` with `{"ok":true}`. Open your project overview in Visibling: a `proxy_smoke` event should appear within a minute.

If nothing arrives:

- Confirm the key starts with `vis_srv_` and is set as a secret on the deployed Worker.
- Confirm `/_v/e` is in the deployed route tree and answers `POST`.
- Check the Worker logs for `[visibling]` warnings from the proxy or the ingest forward.
- Confirm the project you are looking at matches the key's project.

Next: the [browser SDK reference](/docs/browser-sdk), [error tracking and source maps](/docs/errors-and-sourcemaps), the [HTTP ingest API](/docs/api), or [MCP for coding agents](/docs/mcp).
