Show examples in:
List all uploads

To retrieve a collection of uploads, send a GET request to the /uploads endpoint. The collection is paginated, so make sure to iterate over all the pages if you need every record in the collection!

The following table contains the list of all the possible arguments, along with their type, description and examples values.

Pro tip: in case of any doubts you can always inspect the network calls that the CMS interface is doing, as it's using the Content Management API as well!

Query parameters

filter  Optional  object

Attributes to filter uploads

locale  Optional  string  Example: "it"

When filter[query] or field[fields] is defined, filter by this locale. Default: environment's main locale

order_by  Optional  string  Example: "_created_at_DESC,size_ASC"

Fields used to order results. Format: <field_name>_<DIRECTION(ASC|DESC)>. You can pass multiple comma separated rules.

page  Optional  object

Parameters to control offset-based pagination

Returns

Returns an array of upload resource objects.

Examples

Example Fetching one page of results vs. the whole collection

The client.uploads.list() method returns a single page of records, while if you need to iterate over every resource in the collection (and not just the first page of results), you can use the client.uploads.listPagedIterator() method with an async iteration statement, which automatically handles pagination for you.

All the details on how to use list() and listPagedIterator() are outlined on this page.

import { buildClient } from '@datocms/cma-client-node';
async function run() {
const client = buildClient({ apiToken: '<YOUR_API_TOKEN>' });
// this only returns the first page of results:
const uploads = await client.uploads.list();
uploads.forEach((upload) => {
console.log(upload);
});
// this iterates over every page of results:
for await (const upload of client.uploads.listPagedIterator()) {
console.log(upload);
}
}
run();
Example Fetching a filtered list of uploads

You can retrieve a list of uploads filtered by a set of conditions. There are different options and you can combine multiple filters together.

In this example we are filtering by type and size. In particular, we are searching for images bigger than 5MB.

The filtering options are the same as the GraphQL API uploads filters. So please check there all the options.

import { buildClient } from "@datocms/cma-client-node";
async function run() {
// Make sure the API token has access to the CMA, and is stored securely
const client = buildClient({ apiToken: process.env.DATOCMS_API_TOKEN });
const uploads = await client.uploads.list({
filter: {
fields: {
type: {
eq: "image",
},
size: {
gt: 5000000,
},
},
},
});
console.log(uploads);
}
run();