collectionController.js

_104
_104
// List Collections
_104
export const listCollections = async (req, res) => {
_104
try {
_104
const data = await req.webflow.collections.list(req.params.siteId);
_104
res.json(data.collections); // Respond with collection data
_104
} catch (error) {
_104
console.error("Error fetching collections:", error);
_104
res.status(500).send("Failed to fetch collections");
_104
}
_104
};
_104
_104
// Get Collection Details
_104
export const getCollection = async (req, res) => {
_104
try {
_104
const data = await req.webflow.collections.get(req.params.collectionId);
_104
res.json(data); // Respond with collection details
_104
} catch (error) {
_104
console.error("Error fetching collection details:", error);
_104
res.status(500).send("Failed to fetch collection");
_104
}
_104
};
_104
_104
// Helper function to create multiple fields for a given collection
_104
async function createFields(collectionId, fields) {
_104
// Create each field in parallel and return the results
_104
return Promise.all(
_104
fields.map((field) => {
_104
return req.webflow.collections.fields
_104
.create(collectionId, field)
_104
.then((response) => ({
_104
success: true,
_104
field: field.name,
_104
id: response.id,
_104
}))
_104
.catch((error) => ({
_104
success: false,
_104
field: field.name,
_104
error: error.message,
_104
}));
_104
})
_104
);
_104
}
_104
_104
// Create a preset collection and corresponding fields
_104
export const createCollectionWithFields = async (req, res) => {
_104
const siteId = req.params.siteId;
_104
const { name, singularName, slug, fields } = req.body.collection;
_104
_104
// Define details of the new collection
_104
const collectionDetails = {
_104
displayName: name,
_104
singularName: singularName,
_104
slug: slug,
_104
};
_104
_104
try {
_104
// Create the collection in Webflow
_104
const collection = await req.webflow.collections.create(
_104
siteId,
_104
collectionDetails
_104
);
_104
console.log(`Created Collection: ${collection.id} successfully`);
_104
_104
// Create fields for the new collection
_104
const fieldCreationResults = await createFields(collection.id, fields);
_104
console.log("All fields creation attempted.");
_104
_104
// Filter results to separate successful and failed field creations
_104
const successfulFields = fieldCreationResults.filter(
_104
(result) => result.success
_104
);
_104
const failedFields = fieldCreationResults.filter(
_104
(result) => !result.success
_104
);
_104
_104
// Respond with detailed information about the operation
_104
res.status(201).send({
_104
message: "Collection and fields processed.",
_104
collectionId: collection._id,
_104
successfulFields: successfulFields,
_104
failedFields: failedFields,
_104
});
_104
} catch (error) {
_104
console.error("Failed to create collection or fields:", error);
_104
res.status(500).send({
_104
message: "Failed to create collection or fields",
_104
error: error.message,
_104
});
_104
}
_104
};
_104
_104
// Delete Collection
_104
export const deleteCollection = async (req, res) => {
_104
try {
_104
const data = await req.webflow.collections.deleteCollection(
_104
req.params.collectionId
_104
);
_104
res.json(data); // Respond with data
_104
} catch (error) {
_104
console.error("Error deleting collection:", error);
_104
res.status(500).send("Failed to delete collection");
_104
}
_104
};

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.

collectionController.js

_104
_104
// List Collections
_104
export const listCollections = async (req, res) => {
_104
try {
_104
const data = await req.webflow.collections.list(req.params.siteId);
_104
res.json(data.collections); // Respond with collection data
_104
} catch (error) {
_104
console.error("Error fetching collections:", error);
_104
res.status(500).send("Failed to fetch collections");
_104
}
_104
};
_104
_104
// Get Collection Details
_104
export const getCollection = async (req, res) => {
_104
try {
_104
const data = await req.webflow.collections.get(req.params.collectionId);
_104
res.json(data); // Respond with collection details
_104
} catch (error) {
_104
console.error("Error fetching collection details:", error);
_104
res.status(500).send("Failed to fetch collection");
_104
}
_104
};
_104
_104
// Helper function to create multiple fields for a given collection
_104
async function createFields(collectionId, fields) {
_104
// Create each field in parallel and return the results
_104
return Promise.all(
_104
fields.map((field) => {
_104
return req.webflow.collections.fields
_104
.create(collectionId, field)
_104
.then((response) => ({
_104
success: true,
_104
field: field.name,
_104
id: response.id,
_104
}))
_104
.catch((error) => ({
_104
success: false,
_104
field: field.name,
_104
error: error.message,
_104
}));
_104
})
_104
);
_104
}
_104
_104
// Create a preset collection and corresponding fields
_104
export const createCollectionWithFields = async (req, res) => {
_104
const siteId = req.params.siteId;
_104
const { name, singularName, slug, fields } = req.body.collection;
_104
_104
// Define details of the new collection
_104
const collectionDetails = {
_104
displayName: name,
_104
singularName: singularName,
_104
slug: slug,
_104
};
_104
_104
try {
_104
// Create the collection in Webflow
_104
const collection = await req.webflow.collections.create(
_104
siteId,
_104
collectionDetails
_104
);
_104
console.log(`Created Collection: ${collection.id} successfully`);
_104
_104
// Create fields for the new collection
_104
const fieldCreationResults = await createFields(collection.id, fields);
_104
console.log("All fields creation attempted.");
_104
_104
// Filter results to separate successful and failed field creations
_104
const successfulFields = fieldCreationResults.filter(
_104
(result) => result.success
_104
);
_104
const failedFields = fieldCreationResults.filter(
_104
(result) => !result.success
_104
);
_104
_104
// Respond with detailed information about the operation
_104
res.status(201).send({
_104
message: "Collection and fields processed.",
_104
collectionId: collection._id,
_104
successfulFields: successfulFields,
_104
failedFields: failedFields,
_104
});
_104
} catch (error) {
_104
console.error("Failed to create collection or fields:", error);
_104
res.status(500).send({
_104
message: "Failed to create collection or fields",
_104
error: error.message,
_104
});
_104
}
_104
};
_104
_104
// Delete Collection
_104
export const deleteCollection = async (req, res) => {
_104
try {
_104
const data = await req.webflow.collections.deleteCollection(
_104
req.params.collectionId
_104
);
_104
res.json(data); // Respond with data
_104
} catch (error) {
_104
console.error("Error deleting collection:", error);
_104
res.status(500).send("Failed to delete collection");
_104
}
_104
};

Create fields

collectionController.js

_104
_104
// List Collections
_104
export const listCollections = async (req, res) => {
_104
try {
_104
const data = await req.webflow.collections.list(req.params.siteId);
_104
res.json(data.collections); // Respond with collection data
_104
} catch (error) {
_104
console.error("Error fetching collections:", error);
_104
res.status(500).send("Failed to fetch collections");
_104
}
_104
};
_104
_104
// Get Collection Details
_104
export const getCollection = async (req, res) => {
_104
try {
_104
const data = await req.webflow.collections.get(req.params.collectionId);
_104
res.json(data); // Respond with collection details
_104
} catch (error) {
_104
console.error("Error fetching collection details:", error);
_104
res.status(500).send("Failed to fetch collection");
_104
}
_104
};
_104
_104
// Helper function to create multiple fields for a given collection
_104
async function createFields(collectionId, fields) {
_104
// Create each field in parallel and return the results
_104
return Promise.all(
_104
fields.map((field) => {
_104
return req.webflow.collections.fields
_104
.create(collectionId, field)
_104
.then((response) => ({
_104
success: true,
_104
field: field.name,
_104
id: response.id,
_104
}))
_104
.catch((error) => ({
_104
success: false,
_104
field: field.name,
_104
error: error.message,
_104
}));
_104
})
_104
);
_104
}
_104
_104
// Create a preset collection and corresponding fields
_104
export const createCollectionWithFields = async (req, res) => {
_104
const siteId = req.params.siteId;
_104
const { name, singularName, slug, fields } = req.body.collection;
_104
_104
// Define details of the new collection
_104
const collectionDetails = {
_104
displayName: name,
_104
singularName: singularName,
_104
slug: slug,
_104
};
_104
_104
try {
_104
// Create the collection in Webflow
_104
const collection = await req.webflow.collections.create(
_104
siteId,
_104
collectionDetails
_104
);
_104
console.log(`Created Collection: ${collection.id} successfully`);
_104
_104
// Create fields for the new collection
_104
const fieldCreationResults = await createFields(collection.id, fields);
_104
console.log("All fields creation attempted.");
_104
_104
// Filter results to separate successful and failed field creations
_104
const successfulFields = fieldCreationResults.filter(
_104
(result) => result.success
_104
);
_104
const failedFields = fieldCreationResults.filter(
_104
(result) => !result.success
_104
);
_104
_104
// Respond with detailed information about the operation
_104
res.status(201).send({
_104
message: "Collection and fields processed.",
_104
collectionId: collection._id,
_104
successfulFields: successfulFields,
_104
failedFields: failedFields,
_104
});
_104
} catch (error) {
_104
console.error("Failed to create collection or fields:", error);
_104
res.status(500).send({
_104
message: "Failed to create collection or fields",
_104
error: error.message,
_104
});
_104
}
_104
};
_104
_104
// Delete Collection
_104
export const deleteCollection = async (req, res) => {
_104
try {
_104
const data = await req.webflow.collections.deleteCollection(
_104
req.params.collectionId
_104
);
_104
res.json(data); // Respond with data
_104
} catch (error) {
_104
console.error("Error deleting collection:", error);
_104
res.status(500).send("Failed to delete collection");
_104
}
_104
};

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.

collectionController.js

_104
_104
// List Collections
_104
export const listCollections = async (req, res) => {
_104
try {
_104
const data = await req.webflow.collections.list(req.params.siteId);
_104
res.json(data.collections); // Respond with collection data
_104
} catch (error) {
_104
console.error("Error fetching collections:", error);
_104
res.status(500).send("Failed to fetch collections");
_104
}
_104
};
_104
_104
// Get Collection Details
_104
export const getCollection = async (req, res) => {
_104
try {
_104
const data = await req.webflow.collections.get(req.params.collectionId);
_104
res.json(data); // Respond with collection details
_104
} catch (error) {
_104
console.error("Error fetching collection details:", error);
_104
res.status(500).send("Failed to fetch collection");
_104
}
_104
};
_104
_104
// Helper function to create multiple fields for a given collection
_104
async function createFields(collectionId, fields) {
_104
// Create each field in parallel and return the results
_104
return Promise.all(
_104
fields.map((field) => {
_104
return req.webflow.collections.fields
_104
.create(collectionId, field)
_104
.then((response) => ({
_104
success: true,
_104
field: field.name,
_104
id: response.id,
_104
}))
_104
.catch((error) => ({
_104
success: false,
_104
field: field.name,
_104
error: error.message,
_104
}));
_104
})
_104
);
_104
}
_104
_104
// Create a preset collection and corresponding fields
_104
export const createCollectionWithFields = async (req, res) => {
_104
const siteId = req.params.siteId;
_104
const { name, singularName, slug, fields } = req.body.collection;
_104
_104
// Define details of the new collection
_104
const collectionDetails = {
_104
displayName: name,
_104
singularName: singularName,
_104
slug: slug,
_104
};
_104
_104
try {
_104
// Create the collection in Webflow
_104
const collection = await req.webflow.collections.create(
_104
siteId,
_104
collectionDetails
_104
);
_104
console.log(`Created Collection: ${collection.id} successfully`);
_104
_104
// Create fields for the new collection
_104
const fieldCreationResults = await createFields(collection.id, fields);
_104
console.log("All fields creation attempted.");
_104
_104
// Filter results to separate successful and failed field creations
_104
const successfulFields = fieldCreationResults.filter(
_104
(result) => result.success
_104
);
_104
const failedFields = fieldCreationResults.filter(
_104
(result) => !result.success
_104
);
_104
_104
// Respond with detailed information about the operation
_104
res.status(201).send({
_104
message: "Collection and fields processed.",
_104
collectionId: collection._id,
_104
successfulFields: successfulFields,
_104
failedFields: failedFields,
_104
});
_104
} catch (error) {
_104
console.error("Failed to create collection or fields:", error);
_104
res.status(500).send({
_104
message: "Failed to create collection or fields",
_104
error: error.message,
_104
});
_104
}
_104
};
_104
_104
// Delete Collection
_104
export const deleteCollection = async (req, res) => {
_104
try {
_104
const data = await req.webflow.collections.deleteCollection(
_104
req.params.collectionId
_104
);
_104
res.json(data); // Respond with data
_104
} catch (error) {
_104
console.error("Error deleting collection:", error);
_104
res.status(500).send("Failed to delete collection");
_104
}
_104
};

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

_104
_104
// List Collections
_104
export const listCollections = async (req, res) => {
_104
try {
_104
const data = await req.webflow.collections.list(req.params.siteId);
_104
res.json(data.collections); // Respond with collection data
_104
} catch (error) {
_104
console.error("Error fetching collections:", error);
_104
res.status(500).send("Failed to fetch collections");
_104
}
_104
};
_104
_104
// Get Collection Details
_104
export const getCollection = async (req, res) => {
_104
try {
_104
const data = await req.webflow.collections.get(req.params.collectionId);
_104
res.json(data); // Respond with collection details
_104
} catch (error) {
_104
console.error("Error fetching collection details:", error);
_104
res.status(500).send("Failed to fetch collection");
_104
}
_104
};
_104
_104
// Helper function to create multiple fields for a given collection
_104
async function createFields(collectionId, fields) {
_104
// Create each field in parallel and return the results
_104
return Promise.all(
_104
fields.map((field) => {
_104
return req.webflow.collections.fields
_104
.create(collectionId, field)
_104
.then((response) => ({
_104
success: true,
_104
field: field.name,
_104
id: response.id,
_104
}))
_104
.catch((error) => ({
_104
success: false,
_104
field: field.name,
_104
error: error.message,
_104
}));
_104
})
_104
);
_104
}
_104
_104
// Create a preset collection and corresponding fields
_104
export const createCollectionWithFields = async (req, res) => {
_104
const siteId = req.params.siteId;
_104
const { name, singularName, slug, fields } = req.body.collection;
_104
_104
// Define details of the new collection
_104
const collectionDetails = {
_104
displayName: name,
_104
singularName: singularName,
_104
slug: slug,
_104
};
_104
_104
try {
_104
// Create the collection in Webflow
_104
const collection = await req.webflow.collections.create(
_104
siteId,
_104
collectionDetails
_104
);
_104
console.log(`Created Collection: ${collection.id} successfully`);
_104
_104
// Create fields for the new collection
_104
const fieldCreationResults = await createFields(collection.id, fields);
_104
console.log("All fields creation attempted.");
_104
_104
// Filter results to separate successful and failed field creations
_104
const successfulFields = fieldCreationResults.filter(
_104
(result) => result.success
_104
);
_104
const failedFields = fieldCreationResults.filter(
_104
(result) => !result.success
_104
);
_104
_104
// Respond with detailed information about the operation
_104
res.status(201).send({
_104
message: "Collection and fields processed.",
_104
collectionId: collection._id,
_104
successfulFields: successfulFields,
_104
failedFields: failedFields,
_104
});
_104
} catch (error) {
_104
console.error("Failed to create collection or fields:", error);
_104
res.status(500).send({
_104
message: "Failed to create collection or fields",
_104
error: error.message,
_104
});
_104
}
_104
};
_104
_104
// Delete Collection
_104
export const deleteCollection = async (req, res) => {
_104
try {
_104
const data = await req.webflow.collections.deleteCollection(
_104
req.params.collectionId
_104
);
_104
res.json(data); // Respond with data
_104
} catch (error) {
_104
console.error("Error deleting collection:", error);
_104
res.status(500).send("Failed to delete collection");
_104
}
_104
};

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

_104
_104
// List Collections
_104
export const listCollections = async (req, res) => {
_104
try {
_104
const data = await req.webflow.collections.list(req.params.siteId);
_104
res.json(data.collections); // Respond with collection data
_104
} catch (error) {
_104
console.error("Error fetching collections:", error);
_104
res.status(500).send("Failed to fetch collections");
_104
}
_104
};
_104
_104
// Get Collection Details
_104
export const getCollection = async (req, res) => {
_104
try {
_104
const data = await req.webflow.collections.get(req.params.collectionId);
_104
res.json(data); // Respond with collection details
_104
} catch (error) {
_104
console.error("Error fetching collection details:", error);
_104
res.status(500).send("Failed to fetch collection");
_104
}
_104
};
_104
_104
// Helper function to create multiple fields for a given collection
_104
async function createFields(collectionId, fields) {
_104
// Create each field in parallel and return the results
_104
return Promise.all(
_104
fields.map((field) => {
_104
return req.webflow.collections.fields
_104
.create(collectionId, field)
_104
.then((response) => ({
_104
success: true,
_104
field: field.name,
_104
id: response.id,
_104
}))
_104
.catch((error) => ({
_104
success: false,
_104
field: field.name,
_104
error: error.message,
_104
}));
_104
})
_104
);
_104
}
_104
_104
// Create a preset collection and corresponding fields
_104
export const createCollectionWithFields = async (req, res) => {
_104
const siteId = req.params.siteId;
_104
const { name, singularName, slug, fields } = req.body.collection;
_104
_104
// Define details of the new collection
_104
const collectionDetails = {
_104
displayName: name,
_104
singularName: singularName,
_104
slug: slug,
_104
};
_104
_104
try {
_104
// Create the collection in Webflow
_104
const collection = await req.webflow.collections.create(
_104
siteId,
_104
collectionDetails
_104
);
_104
console.log(`Created Collection: ${collection.id} successfully`);
_104
_104
// Create fields for the new collection
_104
const fieldCreationResults = await createFields(collection.id, fields);
_104
console.log("All fields creation attempted.");
_104
_104
// Filter results to separate successful and failed field creations
_104
const successfulFields = fieldCreationResults.filter(
_104
(result) => result.success
_104
);
_104
const failedFields = fieldCreationResults.filter(
_104
(result) => !result.success
_104
);
_104
_104
// Respond with detailed information about the operation
_104
res.status(201).send({
_104
message: "Collection and fields processed.",
_104
collectionId: collection._id,
_104
successfulFields: successfulFields,
_104
failedFields: failedFields,
_104
});
_104
} catch (error) {
_104
console.error("Failed to create collection or fields:", error);
_104
res.status(500).send({
_104
message: "Failed to create collection or fields",
_104
error: error.message,
_104
});
_104
}
_104
};
_104
_104
// Delete Collection
_104
export const deleteCollection = async (req, res) => {
_104
try {
_104
const data = await req.webflow.collections.deleteCollection(
_104
req.params.collectionId
_104
);
_104
res.json(data); // Respond with data
_104
} catch (error) {
_104
console.error("Error deleting collection:", error);
_104
res.status(500).send("Failed to delete collection");
_104
}
_104
};