Correct way to obtain local device ID via REST API?

Hi

I need to get the DeviceID of the local Syncthing instance from a shell script.

I was able to do it via REST API:

BASEURL='http://localhost:8384/rest'
APIKEY='xxxxxxxxxxxxxxxxxxxxxxxxxxx'
curl --silent --header "X-API-Key: $APIKEY" $BASEURL/system/status |
jq '(.myID | split("-")[0]) as $short_id | { full_id: .myID, short_id: $short_id }'

Sample output:

{
  "full_id": "63XRPVV-SRQRZAF-LMQ2C3M-BXJHTZA-KA9CHHE-2EAQ3PU-POFDYNW-JFYEVBN",
  "short_id": "63XRPVV"
}

Just wondering if this is “correct” or if there’s a more concise way to do it that doesn’t require jq

That looks good to me. There is another shortcut posssible, as the device ID is returned in a header value for all requests. This may be quicker to parse.

Neat! I didn’t know that. Beats having to fetch the API key, and also removes the external dependency to jq.

curl -sIo /dev/null http://localhost:8384/rest/noauth/health -w '%header{X-Syncthing-Id}'

Output:

63XRPVV-SRQRZAF-LMQ2C3M-BXJHTZA-KA9CHHE-2EAQ3PU-POFDYNW-JFYEVBN

Thanks!

2 Likes