# How to use Event API?

**URL:** <https://www.thethingsnetwork.org/forum/t/how-to-use-event-api/57252>\
**Category:** The Things Stack v3\
**Created:** [May 27, 2022, 8:14am UTC](https://www.thethingsnetwork.org/forum/t/how-to-use-event-api/57252 "2022-05-27T08:14:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jssani](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/jssani/32/35697_2.png) [@jssani](https://www.thethingsnetwork.org/forum/u/jssani)\
**Post date:** [May 27, 2022, 8:14am UTC](https://www.thethingsnetwork.org/forum/t/how-to-use-event-api/57252/1 "2022-05-27T08:14:54Z")

</div>

I’m trying to make a script using the Event API, but I can’t get it to work. The reason for using it, is that I need to monitor some events on my gateways. I have used the documentation found here

[https://www.thethingsindustries.com/docs/reference/api/events/](https://www.thethingsindustries.com/docs/reference/api/events/)  
[https://www.thethingsindustries.com/docs/getting-started/events/](https://www.thethingsindustries.com/docs/getting-started/events/)

I’m able to get it to work with a curl command

`curl https://tenant.eu1.cloud.thethings.industries/api/v3/events -X POST -H "Authorization: Bearer NNSXS.XXXXXX" -H "Accept: text/event-stream" --data '{"identifiers":[{"gateway_ids":{"gateway_id":"gw139"}}]}'`

Since I want to store some of the events in a database, I have tried to set up a flow in Node-RED and also using a python script with no success.

The python script looks like this

```auto
import requests

auth_token = 'NNSXS.XXXXXX'

header = {'Authorization': 'Bearer ' + auth_token,
          'Accept': 'text/event-stream'}

payload = '{"identifiers":[{"gateway_ids":{"gateway_id":"gw139"}}]}'

url = 'https://tenant.eu1.cloud.thethings.industries/api/v3/events'
response = requests.post(url, headers=header, data=payload)
print(response.json())

```

After doing a little bit of research it looks like I need something called a SSE client, but after trying to use that in python and Node-RED - I still can’t get it to work.

Does anybody have any experience with this or can point me in the right direction 🙂 ?

---

<div class="post-metadata">

**Author:** ![jenishp619](https://www.thethingsnetwork.org/forum/letter_avatar_proxy/v4/letter/j/57b2e6/32.png) [@jenishp619](https://www.thethingsnetwork.org/forum/u/jenishp619)\
**Post date:** [September 1, 2023, 12:58pm UTC](https://www.thethingsnetwork.org/forum/t/how-to-use-event-api/57252/2 "2023-09-01T12:58:14Z")

</div>

I agree to the above questions mine is also not working. Please see the above image

 ![Screenshot 2023-09-01 095430updated](https://www.thethingsnetwork.org/forum/uploads/default/original/3X/8/f/8f38fce9929b7f15b8f90e2d5d83ee2312457798.png)  
Does anyone know why I am getting 400 bad status error. Thingstack CLI command is working for event but having trouble with API v3.

---

<div class="post-metadata">

**Author:** ![bertrik](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/bertrik/32/35697_2.png) [@bertrik](https://www.thethingsnetwork.org/forum/u/bertrik)\
**Post date:** [September 2, 2023, 8:56pm UTC](https://www.thethingsnetwork.org/forum/t/how-to-use-event-api/57252/3 "2023-09-02T20:56:06Z")

</div>

I have a working Java implementation to read from the event api:

> <https://github.com/bertrik/ttn-gateway-collector/tree/master/ttn-gateway-collector/src/main/java/nl/bertriksikken/ttn/eventstream>
>
> //github.com/bertrik/ttn-gateway-collector/tree/master/ttn-gateway-collector/src/main/java/nl/bertriksikken/ttn/eventstream

Things that my code does, specifically for the event stream:

- Use media type “application/json; charset=utf-8”, I suspect this is internally converted to HTTP header “Content-type: application/json; charset=utf-8”
- Set HTTP header “Accept: text/event-stream”
- Set HTTP header “Authorization: Bearer XXX”
- Set a timeout of 0 for the HTTP connection

---

<div class="post-metadata">

**Author:** ![localhorst](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/localhorst/32/45478_2.png) [@localhorst](https://www.thethingsnetwork.org/forum/u/localhorst)\
**Post date:** [December 26, 2023, 7:17pm UTC](https://www.thethingsnetwork.org/forum/t/how-to-use-event-api/57252/4 "2023-12-26T19:17:56Z")

</div>

Hi,  
like jssani I am struggling with the HTTP API for receiving events for a specific gateway.  
Thanks @bertrik for developing a Java client.  
This is my try in doing the same in python:

```auto
    request_data = '''{
    "identifiers":[
      {
         "gateway_ids":{
            "gateway_id":"THE_ID",
            "eui":"XXX04XXXXXC84XXX"
         }
      }
   ]
}'''

    header = {    
        "Authorization": f"Bearer {key}",
        "Accept": "text/event-stream",
        "Content-Type": "application/json; charset=utf-8"
    }

    res = requests.post('https://eu1.cloud.thethings.network/api/v3/events', request_data, headers=header)

    client = sseclient.SSEClient(response)
    for event in client.events():
        pprint.pprint(json.loads(event.data))

```

Unfortunately the HTTP Post will never return. So the SSE client will not be used.

@bertrik Why do you use a timeout of 0 sec? Can you see any problems with my statements in the header?
