# Python API - downlink\_callback

**URL:** <https://www.thethingsnetwork.org/forum/t/python-api-downlink-callback/40573>\
**Category:** Application Development\
**Created:** [October 14, 2020, 2:36pm UTC](https://www.thethingsnetwork.org/forum/t/python-api-downlink-callback/40573 "2020-10-14T14:36:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![goodrun](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/goodrun/32/25844_2.png) [@goodrun](https://www.thethingsnetwork.org/forum/u/goodrun)\
**Post date:** [October 14, 2020, 2:36pm UTC](https://www.thethingsnetwork.org/forum/t/python-api-downlink-callback/40573/1 "2020-10-14T14:36:55Z")

</div>

Hello!

I am trying to write an application which checks for any uplinks and downlinks of one device.  
I already tried the example code which was discussed in this topic  
[https://www.thethingsnetwork.org/forum/t/a-python-program-to-listen-to-your-devices-with-mqtt/9036/6](https://www.thethingsnetwork.org/forum/t/a-python-program-to-listen-to-your-devices-with-mqtt/9036/6)

Furthermore I tried this code which is an adaption of the example code from here ([https://www.thethingsnetwork.org/docs/applications/python/](https://www.thethingsnetwork.org/docs/applications/python/)):

```auto
import time
import ttn

app_id = "xxx"
access_key = "ttn-account.xxx"

def downlink_callback(msg, client):
  print("Downlink from ", msg.dev_id)
  print(msg)

handler = ttn.HandlerClient(app_id, access_key)

# using mqtt client
mqtt_client = handler.data()
mqtt_client.set_downlink_callback(downlink_callback)
mqtt_client.connect()
time.sleep(60)
mqtt_client.close()

# using application manager client
app_client = handler.application()
my_app = app_client.get()
print(my_app)
my_devices = app_client.devices()
print(my_devices)

```

I am not able to receive downlink data. Is there something wrong in my code?  
For uplink\_callbacks it is working.  
Can anyone help?

---

<div class="post-metadata">

**Author:** ![descartes](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/descartes/32/47220_2.png) [@descartes](https://www.thethingsnetwork.org/forum/u/descartes)\
**Post date:** [October 14, 2020, 3:34pm UTC](https://www.thethingsnetwork.org/forum/t/python-api-downlink-callback/40573/3 "2020-10-14T15:34:11Z")

</div>

The TTN library is been deprecated as per the third line with the warning symbol on the page you linked to and the world may have moved on. There are other examples in the forum that are more up to date.

There may be some confusion about which direction is which - uplinks come from the device to you - you send downlinks - so you won’t receive downlink data as such unless you are entering it in the web console or from another source.

---

<div class="post-metadata">

**Author:** ![goodrun](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/goodrun/32/25844_2.png) [@goodrun](https://www.thethingsnetwork.org/forum/u/goodrun)\
**Post date:** [October 15, 2020, 5:06am UTC](https://www.thethingsnetwork.org/forum/t/python-api-downlink-callback/40573/4 "2020-10-15T05:06:01Z")

</div>

Yes, I saw this, but I did not find another API to use for python.

If I am sending confirmed data, I receive a message from gateway after successfull transmission. I expected this to be an uplink which would be visible via MQTT. At least I see it on my gateway.

---

<div class="post-metadata">

**Author:** ![kersing](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/kersing/32/44777_2.png) [@kersing](https://www.thethingsnetwork.org/forum/u/kersing)\
**Post date:** [October 15, 2020, 7:07am UTC](https://www.thethingsnetwork.org/forum/t/python-api-downlink-callback/40573/5 "2020-10-15T07:07:05Z")

</div>

> [@goodrun](#):
>
> did not find another API to use for python

How about the mqtt api?

> [@goodrun](#):
>
> If I am sending confirmed data, I receive a message from gateway after successfull transmission. I expected this to be an uplink which would be visible via MQTT.

1. Please don’t send confirmed data and if you need to keep within the allowed 10 ACKs per day for a device.
2. It is not clear (to me) where you receive the data. If your node transmits (uplink) and you see data in the console you should see data on mqtt as well if subscribed to the right topic(s). At least that is what I’ve been seeing for the past 4+ years.

---

<div class="post-metadata">

**Author:** ![goodrun](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/goodrun/32/25844_2.png) [@goodrun](https://www.thethingsnetwork.org/forum/u/goodrun)\
**Post date:** [October 15, 2020, 8:31am UTC](https://www.thethingsnetwork.org/forum/t/python-api-downlink-callback/40573/6 "2020-10-15T08:31:52Z")

</div>

> [@kersing](#):
>
> How about the mqtt api?

Yes, I tried it with this API, but still do not receive data about the downlink.  
As already mentioned, I used the code from [A Python program to listen to your devices with MQTT](https://www.thethingsnetwork.org/forum/t/a-python-program-to-listen-to-your-devices-with-mqtt/9036/6)  
Instead of looking for uplinks, I look for downlinks.

```
mqttc.subscribe('+/devices/+/down')

```

> [@kersing](#):
>
> Please don’t send confirmed data and if you need to keep within the allowed 10 ACKs per day for a device

I am aware of this. I usually send uncnf, it was just a test to check if I am receiving anything execpt uplinks.

I am running a python script locally on a RPi and want to store uplink and downlink data to a csv. But I only get uplink data.

---

<div class="post-metadata">

**Author:** ![arjanvanb](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/arjanvanb/32/46626_2.png) [@arjanvanb](https://www.thethingsnetwork.org/forum/u/arjanvanb)\
**Post date:** [October 15, 2020, 8:48am UTC](https://www.thethingsnetwork.org/forum/t/python-api-downlink-callback/40573/7 "2020-10-15T08:48:01Z")

</div>

> [@goodrun](#):
>
> `mqttc.subscribe('+/devices/+/down')`

You’ll need `+/devices/+/events/down/sent` (or a broader wildcard, such as `#`).

> [@goodrun](#):
>
> want to store uplink and downlink data to a csv

Do yourself a favor for future debugging and store _all_ events you can get through MQTT. Also, store the time, and the topic you’re getting it on:

> [@MQTT in Node-RED \[HowTo\]](https://www.thethingsnetwork.org/forum/t/mqtt-in-node-red-howto/39909/1):
>
> For logging, one may want to ensure the topic and time are always included:
> 
> ```javascript
> // A file node only saves msg.payload, but +/+/+/events/down/scheduled
> // holds no details about application, device nor timestamp in the
> // MQTT payload, but only: 
> // {"message": {"port": 1, "payload_raw": "BA=="}}
> // Same goes for TTN-published errors:
> // {"error": "Unable to decode payload fields: Internal error: ..."}}
> // So, ensure the topic and time are also saved into the log file:
> msg.payload = {
> topic: msg.topic,
> timestamp: new Date().toISOString(),
> payload: msg.payload,
> error: msg.error
> }
> 
> return msg;
> 
> ```

---

<div class="post-metadata">

**Author:** ![goodrun](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/goodrun/32/25844_2.png) [@goodrun](https://www.thethingsnetwork.org/forum/u/goodrun)\
**Post date:** [October 15, 2020, 8:57am UTC](https://www.thethingsnetwork.org/forum/t/python-api-downlink-callback/40573/8 "2020-10-15T08:57:22Z")

</div>

Tanks a lot! 😇  
It is working with `+/devices/+/events/down/sent`  
I will also include your advice for logging.  
Happy that it is working 🙂
