> For the complete documentation index, see [llms.txt](https://titan-exchange.gitbook.io/titan/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://titan-exchange.gitbook.io/titan/developer-doc/swap-api/reference/direct/stop-stream.md).

# StopStream

Stop an active quote stream by its ID.

**Tells the server to stop sending data on an active stream.** Use it when you no longer need quotes — after executing a swap, when the user navigates away, or before opening a new stream for a different pair.

## Request

{% tabs %}
{% tab title="Rust" %}

```rust
struct StopStreamRequest {
  /// ID of the stream to stop.
  id: u32,
}
```

{% endtab %}

{% tab title="TypeScript" %}

```typescript
interface StopStreamRequest {
  // ID of the stream to stop.
  id: number;
}
```

{% endtab %}
{% endtabs %}

## Response

The server replies with `StreamStopped` containing the ID of the stopped stream.

{% tabs %}
{% tab title="Rust" %}

```rust
struct StopStreamResponse {
  /// Identifier of the stream that was stopped.
  id: u32,
}
```

{% endtab %}

{% tab title="TypeScript" %}

```typescript
interface StopStreamResponse {
  // Identifier of the stream that was stopped.
  id: number;
}
```

{% endtab %}
{% endtabs %}

## Behavior

* **Queued data may still arrive.** After sending `StopStream`, the server may deliver one or more `StreamData` messages that were already queued. Handle them gracefully.
* **`StreamEnd` always follows.** The server sends a `StreamEnd` message once all queued data has been flushed. **Only after receiving `StreamEnd` is the stream fully closed.**
* **Stream IDs do not survive reconnects.** If the WebSocket connection drops, all streams are implicitly ended. Old IDs are no longer valid — do not call `StopStream` for streams from a previous connection.

## Example

```typescript
// Capture stream ID when a quote stream opens
if ('Response' in msg && 'NewSwapQuoteStream' in msg.Response.data) {
  streamId = msg.Response.stream.id;
}

// After receiving quotes, stop the stream
if ('StreamData' in msg && streamId !== undefined) {
  console.log('Got quotes, stopping stream', streamId);
  await sendRequest(ws, requestId++, { StopStream: { id: streamId } });
  streamId = undefined;
}

// Server confirms the stream was stopped
if ('Response' in msg && 'StreamStopped' in msg.Response.data) {
  console.log('Stream stopped:', msg.Response.data.StreamStopped.id);
}

// Stream is fully closed — no more data will arrive for this stream
if ('StreamEnd' in msg) {
  console.log('Stream ended:', msg.StreamEnd.id);
}
```

See [Connection & Negotiation](/titan/developer-doc/swap-api/reference/direct/connection.md#sending-a-request) for `sendRequest` and `decodeMessage` setup.

***

## Related pages

* [NewSwapQuoteStream](/titan/developer-doc/swap-api/reference/direct/new-swap-quote-stream.md) — open a streaming swap quote
* [Error Handling & Reconnect](/titan/developer-doc/swap-api/guides/error-handling.md) — handle connection drops and stream errors
* [Wire Protocol](/titan/developer-doc/swap-api/reference/wire-protocol.md) — MessagePack encoding and message envelope types
