From efb3c055a68356c3ec756c9fb865563f7950b0d6 Mon Sep 17 00:00:00 2001 From: Lennon Day-Reynolds Date: Thu, 8 Feb 2024 08:53:19 -0800 Subject: [PATCH] Central API sample code for CSV export --- docs/api-examples.md | 50 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 docs/api-examples.md diff --git a/docs/api-examples.md b/docs/api-examples.md new file mode 100644 index 0000000..2f3e3de --- /dev/null +++ b/docs/api-examples.md @@ -0,0 +1,50 @@ +--- +title: ZeroTier API Examples +description: "Code snippets and usage examples for the ZeroTier Central API" +--- + +In the examples below use the following placeholder variables to match commonly-needed parameters: + +- `$CENTRAL_TOKEN`: an API token associated with an active account on [Central](https://my.zerotier.com) +- `$NWID`: an active network ID + +# Exporting Data from the Central API + +The examples below are intended to run in a system terminal, and require the following command-line tools: + +- [curl](https://curl.so) +- [jq](https://jqlang.github.io/jq/) + +Each of them will fetch network information and produce CSV as output. You can then import that CSV into your choice of database, spreadsheet, or configuration-management tool(s). + +## List current networks associated with an account + +```sh +curl -s -H "Authorization: token $ZT_TOKEN" \ + "https://api.zerotier.com/api/v1/network" \ + | jq '.[] | [ + .id, + .config.name, + .config.description, + .totalMemberCount, + .config.creationTime, + .config.ipAssignmentPools[0].ipRangeStart, + .config.ipAssignmentPools[0].ipRangeEnd + ]' \ + | jq -rs '.[] | @csv' +``` + +## List network members + +```sh +curl -H "Authorization: token $ZT_TOKEN" \ + "https://api.zerotier.com/api/v1/network/$NWID/member" \ + | jq '.[] | [ + .id, + .lastSeen, + .physicalAddress, + .ipAssignments[0], + .name + ]' \ + | jq -rs '.[] | @csv +```