# How to Subscribe to Real-Time Data Stream using Bitquery API and Python GraphQL Client

**URL:** <https://community.bitquery.io/t/how-to-subscribe-to-real-time-data-stream-using-bitquery-api-and-python-graphql-client/1431>\
**Category:** GraphQL Tutorials\
**Tags:** closed\
**Created:** [March 29, 2023, 2:55pm UTC](https://community.bitquery.io/t/how-to-subscribe-to-real-time-data-stream-using-bitquery-api-and-python-graphql-client/1431 "2023-03-29T14:55:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Divya](https://avatars.discourse-cdn.com/v4/letter/d/13edae/32.png) [@Divya](https://community.bitquery.io/u/Divya)\
**Post date:** [March 29, 2023, 2:55pm UTC](https://community.bitquery.io/t/how-to-subscribe-to-real-time-data-stream-using-bitquery-api-and-python-graphql-client/1431/1 "2023-03-29T14:55:50Z")

</div>

**Step 1: Import required libraries**

```python
from python_graphql_client import GraphqlClient
import asyncio

```

This code block imports the GraphqlClient library and asyncio for asynchronous operations.

**Step 2: Set up the OAuth token**

```python
auth_token = "Bearer YOUR_ACCESS_TOKEN"

```

Set up the OAuth token. Replace `YOUR_ACCESS_TOKEN` with your actual access token.

**Step 3: Set up the GraphQL client**

```python
ws = GraphqlClient(endpoint="wss://streaming.bitquery.io/graphql", headers={"Authorization": auth_token})

```

This code sets up a GraphqlClient instance with the Bitquery WebSocket endpoint and OAuth authorization header.

**Step 4: Define the subscription callback function**

```python
def callback(response):
    print("got new OHLC data:")
    print(response)

```

This function is called every time new data is received from the WebSocket stream. The function prints the response.

**Step 5: Define the GraphQL subscription query**

```python
query = """
 subscription {
    EVM(network: bsc) {
      Transfers {
        Block {
          Number
          Hash
        }
        Transfer {
          Currency {
            Symbol
            Name
          }
          Amount
        }
      }
    }
  }
"""

```

This code block defines the GraphQL subscription query, which is sent to the Bitquery API to subscribe to the Binance Smart Chain network’s Transfer events.

**Step 6: Make an asynchronous subscription request**

```python
asyncio.run(ws.subscribe(query=query, handle=callback))

```

This code block makes an asynchronous subscription request to the Bitquery API with the defined subscription query and the `handle` parameter set to `callback`. The `handle` parameter specifies the function that should be called when new data is received.

**Output on Terminal**

[![](https://us1.discourse-cdn.com/flex016/uploads/bitquery/original/1X/8d19bf724d065c6957e3f8f342d86ed329b6f951.png) ](https://us1.discourse-cdn.com/flex016/uploads/bitquery/original/1X/8d19bf724d065c6957e3f8f342d86ed329b6f951.png)

**Final Code**

```python
import asyncio
from python_graphql_client import GraphqlClient

auth_token = "Bearer YOUR_ACCESS_TOKEN"
ws = GraphqlClient(endpoint="wss://streaming.bitquery.io/graphql", headers={"Authorization": auth_token})

def callback(response):
    print("got new OHLC data:")
    print(response)

async def subscribe_to_transfers():
    query = """
    subscription {
        EVM(network: bsc) {
            Transfers {
                Block {
                    Number
                    Hash
                }
                Transfer {
                    Currency {
                        Symbol
                        Name
                    }
                    Amount
                }
            }
        }
    }
    """
    await ws.subscribe(query=query, handle=callback)

asyncio.run(subscribe_to_transfers())

```

This code uses OAuth for authentication by setting the authorization header with a Bearer token. Make sure to replace `YOUR_ACCESS_TOKEN` with your actual access token.

---

<div class="post-metadata">

**Author:** ![sumittale](https://avatars.discourse-cdn.com/v4/letter/s/9fc29f/32.png) [@sumittale](https://community.bitquery.io/u/sumittale)\
**Post date:** [July 22, 2024, 6:56am UTC](https://community.bitquery.io/t/how-to-subscribe-to-real-time-data-stream-using-bitquery-api-and-python-graphql-client/1431/2 "2024-07-22T06:56:05Z")

</div>

how i get all tokens data

below is my query and i want to get all data in python code can you please help me

subscription MyQuery {  
Solana {  
DEXTradeByTokens(  
where: {Trade: {Currency: {MintAddress: {is: “3B5wuUrMEi5yATD7on46hKfej3pfmd7t1RKgrsN3pump”}}, Side: {Currency: {MintAddress: {is: “So11111111111111111111111111111111111111112”}}}, Dex: {ProgramAddress: {is: “675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8”}}}, Transaction: {Result: {Success: true}}}  
) {  
Block {  
Time  
}  
Trade {  
Currency {  
Name  
Symbol  
}  
Amount  
PriceAgainstSideCurrency: Price  
PriceInUSD  
Side {  
Currency {  
Name  
Symbol  
}  
Amount  
Type  
}  
}  
Transaction {  
Maker: Signer  
Signature  
}  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![Divya](https://avatars.discourse-cdn.com/v4/letter/d/13edae/32.png) [@Divya](https://community.bitquery.io/u/Divya)\
**Post date:** [July 22, 2024, 8:58am UTC](https://community.bitquery.io/t/how-to-subscribe-to-real-time-data-stream-using-bitquery-api-and-python-graphql-client/1431/3 "2024-07-22T08:58:19Z")

</div>

Check this code sample please @sumittale

> <https://gist.github.com/divyasshree-BQ/84bc875bf1c3c3e9088653d5c9b5d7eb>

---

<div class="post-metadata">

**Author:** ![sumittale](https://avatars.discourse-cdn.com/v4/letter/s/9fc29f/32.png) [@sumittale](https://community.bitquery.io/u/sumittale)\
**Post date:** [July 22, 2024, 10:43am UTC](https://community.bitquery.io/t/how-to-subscribe-to-real-time-data-stream-using-bitquery-api-and-python-graphql-client/1431/4 "2024-07-22T10:43:49Z")

</div>

i have one problem when i used the below code in my vscode then the code not giving me the correct data and when i run the query on bitquery ide then the code giving me the right data  
why this is happening can you please help me.

import asyncio  
from python\_graphql\_client import GraphqlClient  
import time

auth\_token = “Bearer ory\_at\_ejrB\_FthB0Q35uZfO–6P8LXpRPjLKKYSW04wNyY6iA.HYMJAp9bSjFhB1KAgaPn3PazCbiSCCA\_cwb5bBu2GmA”  
ws = GraphqlClient(endpoint=“wss://[streaming.bitquery.io/eap](http://streaming.bitquery.io/eap)”, headers={“Authorization”: auth\_token})

def callback(response):  
print(“got new OHLC data:”)  
print(response)  
time.sleep(20)

async def subscribe\_to\_transfers():  
query = “”"  
subscription MyQuery {  
Solana {  
DEXTradeByTokens(  
where: {Trade: {Currency: {MintAddress: {is: “B6DuEXoYuzqYTTscrGUGDCK1t9tGkH7Ud1yQq65AC6A8”}}, Side: {Currency: {MintAddress: {is: “So11111111111111111111111111111111111111112”}}}, Dex: {ProgramAddress: {is: “675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8”}}}, Transaction: {Result: {Success: true}}}  
) {  
Block {  
Time  
}  
Trade {  
Currency {  
Name  
Symbol  
}  
Amount  
PriceAgainstSideCurrency: Price  
PriceInUSD  
Side {  
Currency {  
Name  
Symbol  
}  
Amount  
Type  
}  
}  
Transaction {  
Maker: Signer  
Signature  
}  
}  
}  
}  
“”"  
await ws.subscribe(query=query, handle=callback)

asyncio.run(subscribe\_to\_transfers())

---

<div class="post-metadata">

**Author:** ![sumittale](https://avatars.discourse-cdn.com/v4/letter/s/9fc29f/32.png) [@sumittale](https://community.bitquery.io/u/sumittale)\
**Post date:** [July 22, 2024, 11:02am UTC](https://community.bitquery.io/t/how-to-subscribe-to-real-time-data-stream-using-bitquery-api-and-python-graphql-client/1431/5 "2024-07-22T11:02:46Z")

</div>

i have one problem when i used the below code in my vscode then the code not giving me the correct data and when i run the query on bitquery ide then the code giving me the right data  
why this is happening can you please help me.

import asyncio  
from python\_graphql\_client import GraphqlClient  
import time

auth\_token = “Bearer ory\_at\_ejrB\_FthB0Q35uZfO–6P8LXpRPjLKKYSW04wNyY6iA.HYMJAp9bSjFhB1KAgaPn3PazCbiSCCA\_cwb5bBu2GmA”  
ws = GraphqlClient(endpoint=“wss://streaming.bitquery.io/eap”, headers={“Authorization”: auth\_token})

def callback(response):  
print(“got new OHLC data:”)  
print(response)  
time.sleep(20)

async def subscribe\_to\_transfers():  
query = “”"  
subscription MyQuery {  
Solana {  
DEXTradeByTokens(  
where: {Trade: {Currency: {MintAddress: {is: “B6DuEXoYuzqYTTscrGUGDCK1t9tGkH7Ud1yQq65AC6A8”}}, Side: {Currency: {MintAddress: {is: “So11111111111111111111111111111111111111112”}}}, Dex: {ProgramAddress: {is: “675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8”}}}, Transaction: {Result: {Success: true}}}  
) {  
Block {  
Time  
}  
Trade {  
Currency {  
Name  
Symbol  
}  
Amount  
PriceAgainstSideCurrency: Price  
PriceInUSD  
Side {  
Currency {  
Name  
Symbol  
}  
Amount  
Type  
}  
}  
Transaction {  
Maker: Signer  
Signature  
}  
}  
}  
}  
“”"  
await ws.subscribe(query=query, handle=callback)

asyncio.run(subscribe\_to\_transfers())

---

<div class="post-metadata">

**Author:** ![Divya](https://avatars.discourse-cdn.com/v4/letter/d/13edae/32.png) [@Divya](https://community.bitquery.io/u/Divya)\
**Post date:** [July 22, 2024, 1:52pm UTC](https://community.bitquery.io/t/how-to-subscribe-to-real-time-data-stream-using-bitquery-api-and-python-graphql-client/1431/6 "2024-07-22T13:52:28Z")

</div>

@sumittale please reset your token, since it is exposed in this post. Regarding the data, can you share an example of the error you are talking about?

---

<div class="post-metadata">

**Author:** ![Divya](https://avatars.discourse-cdn.com/v4/letter/d/13edae/32.png) [@Divya](https://community.bitquery.io/u/Divya)\
**Post date:** [January 6, 2025, 7:10am UTC](https://community.bitquery.io/t/how-to-subscribe-to-real-time-data-stream-using-bitquery-api-and-python-graphql-client/1431/7 "2025-01-06T07:10:44Z")

</div>

Updated Code Samples: [Building with WebSockets: Code Samples in Python & JavaScript | Blockchain Data API (V2)](https://docs.bitquery.io/docs/subscriptions/examples/)
