How to download an Archive using Ion REST API

I am trying to download an archive using Cesium Ion API.

Below is my code

const octetStream = await request({
        url: `https://api.cesium.com/v1/archives/${archiveId}/download`,
        headers: { Authorization: `Bearer ${cesiumIonAccessToken}` },
        json: true
    });

const buffer = Buffer.from(octetStream, "binary");
fs.writeFileSync("test.zip", buffer);

When I open test.zip, I see an error message, “test.zip is invalid”.
But when I open test.zip in https://hexed.it/ and try to identify

In the above code, the variable octetStream is a string.

Thank you.

Hi,
Looking at the code my first though is that it does not need json: true since it is not expecting json data.

Please let us know if that resovles your issue. If you are still having trouble with this, please let us know:

  1. What version of Node JS are you using?
  2. Are you able to provide a full code sample? (your sample is missing a require/import statements)
  3. What asset id are you trying to download?

1 NodeJS version : v22.13.1
2 Sure

const request = require("request-promise");
const fs = require("fs");

async function download() {
    const archiveId = 144513;
    const cesiumIonAccessToken = "xxxxxxxxxxxx";

    const octetStream = await request({
        url: `https://api.cesium.com/v1/archives/${archiveId}/download`,
        headers: { Authorization: `Bearer ${cesiumIonAccessToken}` },
        json: true
    });

    const buffer = Buffer.from(octetStream, "binary");

    fs.writeFileSync("test.zip", buffer);

    const assetId = 3272521;

    const response = await request({
        url: `https://api.cesium.com/v1/assets/${assetId}`,
        headers: {
            'Authorization': `Bearer ${cesiumIonAccessToken}`
        },
        json: true
    });

    console.log(response);

    console.log("done");
}

download();

3 asset’s Id : 3272521

Thank you

You are using request-promise which has been deprecated for some time. I’m not familar with that API so here is an example of how you would fetch and save the data using the fetch API. Using this method I was able to download and open a zip file.

const response = await fetch(
  `https://api.cesium.com/v1/archives/${archiveId}/download`,
  { headers: { Authorization: `Bearer ${cesiumIonAccessToken}` } }
);

if (!response.ok) {
  console.error(response.statusText);
  return;
}
const array = await response.arrayBuffer();
const buffer = Buffer.from(array);
fs.writeFileSync("test.zip", buffer);

A “+1” for not using the deprecated library.

But iff you want to use request-promise, I think that

        //json: true // removing this
        encoding: null // adding this

should do the trick.

1 Like

@Marco13, your code works!
Thank you very much.

1 Like