List collections

Our first method, listCollections, is used to fetch and return the list of collections for an authorized site using the list collections endpoint. It expects a siteId as a URL parameter, which our frontend should send in the request. If successful, it will respond with a JSON object containing the list of collections.

Get collection details

Get collection details will get the details of a single collection using the get collection endpoint. It expects a collectionId as a URL parameter, which our frontend should send in the request. If successful, it will respond with a JSON object with additional details about the collection, most importantly the fields array which contains the schema, or the fields that belong to the collection.

Create fields

This is a helper function that creates multiple fields for a given collection using the create fields endpoint. In our frontend we've created a list of preset collections in CollectionPresets.js. Each preset has a collection object that contains the collection details, as well as a list of fields that each have their own name, type, and other properties.

This helper function expects a collectionId and a list of fields, and will create each field for the given collection. So we must first create the collection, and then use the collectionId to create the fields.

Create a collection with fields

This is the main function that creates a collection and the corresponding fields. It expects a siteId, as well as a collection object that contains the collection details and a list of fields from a selected preset from CollectionPresets.js.

It will first create the collection using the create collection endpoint, then use the helper function createFields to create multiple fields. Finally, it will respond with the results of each successful field creation.

Delete a collection

This function deletes a collection using the delete collection endpoint. It expects a collectionId as a URL parameter, which our frontend should send in the request. The delete endpoint does not return any data, so we will just respond with a status of 200.

collectionController.js
ExpandClose
1

2
// List Collections
3
export const listCollections = async (req, res) => {
4
try {
5
const data = await req.webflow.collections.list(req.params.siteId);
6
res.json(data.collections); // Respond with collection data
7
} catch (error) {
8
console.error("Error fetching collections:", error);
9
res.status(500).send("Failed to fetch collections");
10
}
11
};
12

13
// Get Collection Details
14
export const getCollection = async (req, res) => {
15
try {
16
const data = await req.webflow.collections.get(req.params.collectionId);
17
res.json(data); // Respond with collection details
18
} catch (error) {
19
console.error("Error fetching collection details:", error);
20
res.status(500).send("Failed to fetch collection");
21
}
22
};
23

24
// Helper function to create multiple fields for a given collection
25
async function createFields(collectionId, fields) {
26
// Create each field in parallel and return the results
27
return Promise.all(
28
fields.map((field) => {
29
return req.webflow.collections.fields
30
.create(collectionId, field)
31
.then((response) => ({
32
success: true,
33
field: field.name,
34
id: response.id,
35
}))
36
.catch((error) => ({
37
success: false,
38
field: field.name,
39
error: error.message,
40
}));
41
})
42
);
43
}
44

45
// Create a preset collection and corresponding fields
46
export const createCollectionWithFields = async (req, res) => {
47
const siteId = req.params.siteId;
48
const { name, singularName, slug, fields } = req.body.collection;
49

50
// Define details of the new collection
51
const collectionDetails = {
52
displayName: name,
53
singularName: singularName,
54
slug: slug,
55
};
56

57
try {
58
// Create the collection in Webflow
59
const collection = await req.webflow.collections.create(
60
siteId,
61
collectionDetails
62
);
63
console.log(`Created Collection: ${collection.id} successfully`);
64

65
// Create fields for the new collection
66
const fieldCreationResults = await createFields(collection.id, fields);
67
console.log("All fields creation attempted.");
68

69
// Filter results to separate successful and failed field creations
70
const successfulFields = fieldCreationResults.filter(
71
(result) => result.success
72
);
73
const failedFields = fieldCreationResults.filter(
74
(result) => !result.success
75
);
76

77
// Respond with detailed information about the operation
78
res.status(201).send({
79
message: "Collection and fields processed.",
80
collectionId: collection._id,
81
successfulFields: successfulFields,
82
failedFields: failedFields,
83
});
84
} catch (error) {
85
console.error("Failed to create collection or fields:", error);
86
res.status(500).send({
87
message: "Failed to create collection or fields",
88
error: error.message,
89
});
90
}
91
};
92

93
// Delete Collection
94
export const deleteCollection = async (req, res) => {
95
try {
96
const data = await req.webflow.collections.deleteCollection(
97
req.params.collectionId
98
);
99
res.json(data); // Respond with data
100
} catch (error) {
101
console.error("Error deleting collection:", error);
102
res.status(500).send("Failed to delete collection");
103
}
104
};