Search code examples
meraki-api

How can I batch get-network-sm-devices in Meraki SM API v1?


I am converting my call to Meraki API v0 to v1 because v0 has sunset.

I am trying to retrieve all devices in batches for SM devices. Specifically this API call: https://developer.cisco.com/meraki/api/get-network-sm-devices/

In the v0 version of this API, params and the response looked like the following.

Params 1

batchSize: 3

Request 1

{
  "batchToken": "123abc",
  "devices": [{"id": "device1"}, {"id": "device2"}, {"id": "device3"}]
}

I see a batchToken, so there are more devices to retrieve.

Params 2

batchToken: "123abc"

Request 2 Response

{
  "batchToken": "456def",
  "devices": [{"id": "device4"}, {"id": "device5"}, {"id": "device6"}
}

Awesome! And there are still more to retrieve so now I use the new batchToken until there isn't one. This was in v0 though.

In API v1, the same request looks like this.

Params 1

perPage: 3

Request 1 Response

[{"id": "device1"}, {"id": "device2"}, {"id": "device3"}]

There is no batchToken. If it was documented, I didn't understand how to implement the same behavior as in v0. If it's not possible to do this, which API call would I need to see how many devices there are total in this network so I could break my requests up on my own?

Thank you!


Solution

  • In get-network-sm-devices endpoint API for V1 we can use the following approach:

    1. Instead of "batchSize" query parameter we use "perPage". It's the number of entries per page returned. The acceptable range is 3 - 1000. Default value - 1000.
    2. If there are 3000 devices, for instance, we can use "startingAfter" query parameter with the default value for "perPage". It can be a timestamp or device ID, but it's not limited to those.

    Be careful, "startingAfter" parameter does not support "None" value. It will return an error. So, for your case, you can create a variable that will contain the last device ID from the first request and pass it as a query parameter to the next one.

    Have a nice day!