Skip to content

Instantly share code, notes, and snippets.

@fostimus
Created November 17, 2023 17:48
Show Gist options
  • Save fostimus/f214f1fddc6fbbd6adcd31c56d8b3ec1 to your computer and use it in GitHub Desktop.
Save fostimus/f214f1fddc6fbbd6adcd31c56d8b3ec1 to your computer and use it in GitHub Desktop.
Downloads all zip files for completed etch packets in your Anvil org
// Fetches completed EtchPackets from your organization and outputs all download
// URLs. More information:
//
// https://www.useanvil.com/docs/api/graphql/reference/#definition-EtchPacket
// https://www.useanvil.com/docs/api/e-signatures/#downloading-documents
const fs = require("fs");
const Anvil = require("@anvilco/anvil");
const client = new Anvil({
apiKey: process.env.ANVIL_API_TOKEN,
});
const main = async () => {
const result = await client.requestGraphQL({
query: `
query Organization (
$organizationSlug: String!,
$status: [String],
$isArchived: Boolean,
$limit: Int,
$offset: Int
) {
organization (organizationSlug: $organizationSlug) {
eid
etchPackets (
status: $status,
isArchived: $isArchived,
limit: $limit,
offset: $offset
) {
items {
eid,
status
name
documentGroup {
eid
files
downloadZipURL
}
}
}
}
}
`,
variables: {
organizationSlug: "demo", // Your org slug from the URL, e.g. https://app.useanvil.com/org/demo
status: ["completed"], // One or more of 'created', 'in-progress', 'ready-to-sign', 'awaiting-signatures', 'completed',
limit: 200, // Number of items per page - update to be the number of completed etch packets you have
offset: 1, // Page number, 1 based
},
});
const statusCode = result.statusCode;
const httpErrors = result.errors;
const graphqlErrors = result.data?.errors;
const resultObject = result.data?.data?.organization;
resultObject.etchPackets.items.forEach((item, idx) => {
client.downloadDocuments(item.documentGroup.eid).then((res) => {
// create a completed-packets directory if it doesn't exist
if (!fs.existsSync("completed-packets")) {
fs.mkdirSync("completed-packets");
}
fs.writeFile(`completed-packets/${item.name}.zip`, res.data, (err) => {
if (err) {
console.error(err);
throw err;
}
});
});
});
return true;
};
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment