Get all images from asset library for a certain subfolder (or folder) using Admin API

I’m wondering if it’s possible to query the asset library to get all images from a subfolder, preferably of a specific mimetype.

Here’s my code, this code returns “any” images if you get rid of the variables passed to the query, however it doesn’t work if I add in the mimetype or folder id

const assetsQuery = query GetSvgAssets($q: JSONObject) {
  assets(
    input: {
      query: $q,
      sort: {createdDate: “descending”}
    }
  ) {
    id
    name
    url
    bytes
    lastUsed
  }
};

const assetsRequestBody = {
  query: assetsQuery,
  variables: {
    q: {
      folderId: { $eq: folderId },
      mimeType: { $eq: ‘image/svg+xml’ },
    },
  },
};

The documentation doesn’t provide enough examples and detail to help so far.

Hello @elir-usafacts,

The reason your first query fails with folderId and mimeType is because those fields don’t exist directly on the Asset object in the GraphQL schema.

Instead:

  • The folder is stored as an array of folders (IDs).

  • The mimeType is stored as type (e.g., "image/svg+xml").

So your query should look like this:

query ($input: QueryAssetsInput!) {
  assets(input: $input) {
    id
    name
    url
    type
    folders
    createdDate
  }
}
{
  "input": {
    "query": {
      "type": { "$eq": "image/svg+xml" },
      "folders": { "$in": ["2537151df1354a728576310e86bae161"] }
    },
    "sort": { "createdDate": -1 },
    "limit": 50
  }
}

This will return only SVG assets inside the specified folder.

You can also test this directly in a GraphQL editor using the endpoint:
https://cdn.builder.io/api/v2/admin

Here’s a full example query that we tested and confirmed works:

{
  "query": "query ($input: QueryAssetsInput!) { assets(input: $input) { id name ownerId folders createdDate type url } }",
  "variables": {
    "input": {
      "query": {
        "type": { "$eq": "image/svg+xml" },
        "folders": { "$in": ["2537151df1354a728576310e86bae161"] }
      },
      "sort": {
        "createdDate": -1
      },
      "limit": 20
    }
  }
}

We hope this helps!

Thanks,

That worked, thank you. I couldn’t use the builder explorer as it was throwing exceptions. Would it be possible to add these shapes of these endpoints to documentation?

Hello @elir-usafacts,

I’m glad to hear that worked for you!

We’ve submitted a request to update the documentation with a working example that includes the shapes of these endpoints. You can expect the docs to be updated in the coming days.

Thank you for bringing this to our attention—it will help improve the experience for everyone.

Thanks,