# Cancelling a Subscription

This section provides step-by-step guides to canceling stream subscriptions using each supported protocol: Websocket and gRPC.

Websocket

If you need to cancel a subscription, pass the subscription ID to the **`unsubscribe`** call.

#### **Example**

{% tabs %}
{% tab title="Gateway" %}
Unsubscribe call example for the Gateway in Python (version 3.7 or higher):

```python
import asyncio, ssl, json, websockets

async def main():
    auth_key = "YOUR_AUTHORIZATION_HEADER"
    
    async with websockets.connect(
        'ws://127.0.0.1:28333/ws',
        header=["Authorization:{}".format(auth_key)],            
        sslopt={"cert_reqs": ssl.CERT_NONE},        
        ) as websocket:
            subscribe_request = {
                "jsonrpc": "2.0",
                "id": 1,
                "method": "subscribe",
                "params": ["newTxs", {"include": ["tx_hash"]}]
            }
            await websocket.send(json.dumps(subscribe_request))
            response = await websocket.recv()
            subscription_id = json.loads(response)["result"]
    
            unsubscribe_request = {
                "jsonrpc": "2.0",
                "id": 2,
                "method": "unsubscribe",
                "params": [subscription_id]
            }
            await websocket.send(json.dumps(unsubscribe_request))
            response = await websocket.recv()
            print("Unsubscribed successfully.")
    
if __name__ == '__main__':
    asyncio.run(main())
```

{% endtab %}

{% tab title="Cloud-API" %}
Unsubscribe call example for the Cloud-API in Python (version 3.7 or higher):

```python
import asyncio, ssl, json, websockets

async def main():
    auth_key = "YOUR_AUTHORIZATION_HEADER"
    
    async with websockets.connect(
        'wss://virginia.eth.blxrbdn.com/ws',
        header=["Authorization:{}".format(auth_key)],            
        sslopt={"cert_reqs": ssl.CERT_NONE},        
        ) as websocket:
            subscribe_request = {
                "jsonrpc": "2.0",
                "id": 1,
                "method": "subscribe",
                "params": ["newTxs", {"include": ["tx_hash"]}]
            }
            await websocket.send(json.dumps(subscribe_request))
            response = await websocket.recv()
            subscription_id = json.loads(response)["result"]
    
            unsubscribe_request = {
                "jsonrpc": "2.0",
                "id": 2,
                "method": "unsubscribe",
                "params": [subscription_id]
            }
            await websocket.send(json.dumps(unsubscribe_request))
            response = await websocket.recv()
    
            print("Unsubscribed successfully.")
    
if __name__ == '__main__':
    asyncio.run(main())
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.bloxroute.com/bsc/streams/working-with-streams/cancelling-a-subscription.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
