API Q & A topic

Hello,

Is there an API to get my registered gateways data such as their status (connected or not) , their location and eventually their performance…

Thanks and best regards.

2 Likes

Here is a API documentation: https://www.thethingsnetwork.org/docs/network/account/api.html#endpoints

But I can’t exchange access key for access token to use this API. So we can try to solve it together.

Update: I got access token. Here is a strategy to obtain access token:

  1. Download ttnctl https://www.thethingsnetwork.org/docs/network/cli/quick-start.html
  2. Login your account with ttnctl:
    2.1 visit https://account.thethingsnetwork.org/ and press ‘ttnctl access code’
    2.2 run ttnctl command ‘ttnctl user login ’
  3. Register a client with ttnctl:
    3.1 run ttnctl command ‘ttnctl client request -h’ and follow instruction
    3.2 when successfully requested a client run ‘ttnctl client list’ and copy your SECRET
  4. Obtain access token according to this instruction https://www.thethingsnetwork.org/docs/network/account/authentication.html#exchanging-an-access-key-for-an-access-token

Now I have a problem while requesting a list of gateways https://www.thethingsnetwork.org/docs/network/account/api.html#get-gateways

Response message:

{
“code”: 401,
“error”: “User MY_CLIENT_ID not found”
}

Does anyone know how to resolve this problem?

I’m rather interested if there is / will be a way to get stats of gateways using an API. The API there just has basic information including the location. However that API doesn’t seem to let you even see if it’s online.

Does API return any data by this endpoint https://account.thethingsnetwork.org/api/v2/gateways ?

Hi @Ryanteck, I use curl to the TTN NOC server to get realtime gateway status. The following is an example of me doing a curl to get status of one of my gateways about 10 minutes ago. I have re-formatted the JSON for readability.

[root@xxx]# curl http://noc.thethingsnetwork.org:8085/api/v2/gateways/eui-b827ebfffe031022
{
“timestamp”:“2018-03-14T09:00:00.701889865Z”,
“uplink”:“7974”,
“downlink”:“5”,
“location”:{“latitude”:57.124714,“longitude”:-2.164626,“source”:“REGISTRY”},
“platform”:“IMST + Rpi”,
“gps”:{“latitude”:57.124714,“longitude”:-2.164626,“source”:“REGISTRY”},
“time”:“1521018000701889865”,
“rx_ok”:7974,
“tx_in”:5
}
[root@xxx]#

The “timestamp” is, I think, when the gateway was last seen.
The access is open; no key, user/pwd, etc. needed.
Timestamp, counters, etc, match those on the TTN web console for the gateway.

I realise that curl isn’t really an API but you should be able to adapt this method for use from any platform.

1 Like

That’s actually ideal for what I need. Thanks for the heads up!

I need to automatically register gateways on the TTN though Account Server API.
I got an access_token at this endpoint https://account.thethingsnetwork.org/api/v2/applications/token

Here is decrypted details of my JWT:

{
  "sub": "dev_spd_rnd",
  "iss": "ttn-account-v2",
  "iat": 1521026647,
  "client": "xelba-client5",
  "scope": [
    "apps:dev_spd_rnd"
  ],
  "apps": {
    "dev_spd_rnd": [
      "devices",
      "settings",
      "messages:up:r",
      "messages:down:w"
    ]
  },
  "interchangeable": false,
  "exp": 1521030257
}

Here is example request to get a list of available gateways:

curl -X GET
https://account.thethingsnetwork.org/api/v2/gateways
-H ‘authorization: Bearer MY_ACCESS_TOKEN
-H ‘cache-control: no-cache’
-H ‘content-type: application/json’

Response:

{
    "code": 401,
    "error": "User dev_spd_rnd not found"
}

What is the reason I can’t list gateways or register a new gateway over this API?

How to obtain access token with scope ‘apps’ and ‘gateways’?

Here is example cURL request:

curl -X POST \
  https://account.thethingsnetwork.org/api/v2/applications/token \
  -H 'authorization: Basic <MY_TOKEN>' \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -d '{
  "username": "<MY_APP_ID>",
  "password": "<MY_APP_ACCESS_KEY>",
  "grant_type": "password"
}'

The token, which return by this request has only one scope:

"scope": [
    "apps:<MY_APP_ID>"
  ]

But my Client has different scopes:

[apps gateways profile components]

How could I get access token with scope ‘apps’ and ‘gateways’?