List collection items

This function returns a list of items for a collection using the list items endpoint. It expects a collectionId as a URL parameter, which our frontend should send in the request.

Webflow offers query parameters to filter and sort the items returned. Including name, slug, and lastPublished. These filters are useful for narrowing down the list of items returned.

Pagination

For larger collections, you can also paginate through the items using the offset parameters returned in the response. For example, if you have 1000 items in a collection, your first request may return the pagination object {"limit": 100, "offset": 0, total: 1000}.

To get the next set of 100 items, your second request should add a value of 1 to the offset value from the previous response in our example our new offset would be 1. Loop this process until you have retrieved all items.

Create collection item

This function creates a new item in a collection using the create item endpoint. It also expects a collectionId as a URL parameter, as a body payload for the item that includes details like:

  • isDraft: Indicates whether the item is a draft or should be published upon site publish. This defaults to true.
  • isArchived: Indicates whether the item is archived. This defaults to false.
  • fieldData: An object containing the field values for the item with required fields for name and slug. You can add additional fields and values as needed based on the collection schema.

On success, this function returns the new item object with the id and other details.

Create multiple items

The create item endpoint supports creating multiple items at once by sending an array named items in the payload. Each item in the array should include the same fields as a single item payload.

If your site uses localization, you can create items in multiple locales via the create bulk items endpoint, which requires a collection_id as a path parameter.

Delete collection item

This function deletes an item in a collection using the delete item endpoint. It expects a collectionId and itemId as URL parameters, which our frontend should send in the request.

Since this endpoint returns a 204 status code on success, we can just return a success message to the frontend.

itemsController.js
ExpandClose
1
// List collection items
2
export const listItems = async (req, res) => {
3
try {
4
const data = await req.webflow.collections.items.listItems(
5
req.params.collectionId
6
);
7
res.json(data.items);
8
} catch (error) {
9
console.error("Error fetching collection items:", error);
10
res.status(500).send("Failed to fetch collection items");
11
}
12
};
13

14
// Create collection Item
15
export const createItem = async (req, res) => {
16
try {
17
const data = await req.webflow.collections.items.createItem(
18
req.params.collectionId,
19
req.body
20
);
21
res.json(data);
22
} catch (error) {
23
console.error("Error creating collection item:", error);
24
res.status(500).send("Failed to create collection item");
25
}
26
};
27

28
// Delete Collection Item
29
export const deleteItem = async (req, res) => {
30
try {
31
const data = await req.webflow.collections.items.deleteItem(
32
req.params.collectionId,
33
req.params.itemId
34
);
35
res.status(200).send("Item deleted successfully");
36
} catch (error) {
37
console.error("Error deleting item:", error);
38
res.status(500).send("Failed to delete item");
39
}
40
};