> ## Documentation Index
> Fetch the complete documentation index at: https://docs.launchblitz.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Connection

> Authenticate to the LaunchBlitz transaction socket with an API key and keep the connection alive.

The transaction API is a WebSocket service.

## URL

```text theme={null}
wss://tx.launchblitz.ai?key=YOUR_API_KEY
```

`key` is required. The backend accepts either a LaunchBlitz session token or a user API key, but direct integrations should use an API key.

## Authentication

* The socket reads the `key` query parameter during the WebSocket handshake.
* Missing `key` values are rejected at handshake time.
* Invalid keys do not receive the initial `connected` message.
* An API key authenticates as the LaunchBlitz user that owns it, so requests run with that user’s wallets, tier, referral settings, and permissions.

## Initial message

After a successful connection, the server sends a plain text frame:

```text theme={null}
connected
```

Wait for that message before you start sending transaction envelopes.

## Keepalives

The socket supports both text heartbeat messages and standard WebSocket ping/pong frames.

* The server sends a text `ping` every 30 seconds.
* Your client should reply with text `pong`.
* If your client sends text `ping`, the server replies with text `pong`.
* If your client sends a WebSocket ping frame, the server replies with a pong frame.
* The server disconnects the client after 20 missed heartbeat intervals.

## Recommended client behavior

* Reply to every incoming `ping` immediately.
* Optionally send your own `ping` on a shorter interval to measure latency.
* Reconnect on close and resubscribe your local request tracking state.

## JavaScript example

```js theme={null}
const apiKey = process.env.LAUNCHBLITZ_API_KEY;
const ws = new WebSocket(`wss://tx.launchblitz.ai?key=${encodeURIComponent(apiKey)}`);

ws.onopen = () => {
  console.log("socket open");
};

ws.onmessage = (event) => {
  if (event.data === "connected") {
    console.log("authenticated");
    return;
  }

  if (event.data === "ping") {
    ws.send("pong");
    return;
  }

  const message = JSON.parse(event.data);
  console.log(message.msg_type, message.body);
};

ws.onclose = () => {
  console.log("socket closed");
};

setInterval(() => {
  if (ws.readyState === WebSocket.OPEN) {
    ws.send("ping");
  }
}, 5000);
```

## Session identifiers in responses

Transaction responses include `body.session_key`, but that value is the server-side connection identifier for the authenticated session, not your raw API key. Use your own request `identifier` fields to correlate requests.
