How to use Event API?

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/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

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 :slight_smile: ?

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


Does anyone know why I am getting 400 bad status error. Thingstack CLI command is working for event but having trouble with API v3.

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

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

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:

    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?