Creating an item
How to create an item
To create a new item of a particular design, you must specify the designCode
and which collection
the new item will belong to. Additionally, you must supply values for any attributes
that are required by the design.
For more information, see:
-
Creating items in the Alloy Web guide
Example JavaScript
This example demonstrates how to create an item of the Waste Containers design (included with a module). The asset is in active use, so it belongs in the Live collection.
For completeness, the request specifies the new item's icon
and colour
. If these optional properties aren't specified, their values are determined by the item's design.
The request includes values for six attributes
. The first two (Type and Geometry) are required by the item's design, so values must be supplied during item creation.
Waste containers can be organised into groups for easier management. Therefore, the request specifies an item of the Waste Groups design as a parent
of the new item.
// load a http request library
const axios = require('axios');
// enter your api key here!
const apiKey = 'e0e3a4ef-5ec8-4e05-ac25-a34adadf4a80';
// request to create an item
axios({
method: 'POST',
url: `https://api.uk.alloyapp.io/api/item`,
headers: { Authorization: `Bearer ${apiKey}` },
data: {
designCode: 'designs_wasteContainers',
collection: 'Live',
icon: 'icon-resource-waste-point',
colour: '#4aa36b',
attributes: [
{
attributeCode: 'attributes_wasteContainersType',
value: ['5ebe834b76d77b004d8111f2'],
},
{
attributeCode: 'attributes_itemsGeometry',
value: { type: 'Point', coordinates: [-0.71949, 52.588322] },
},
{
attributeCode: 'attributes_wasteContainersRfid',
value: '4F4835423F53',
},
{
attributeCode: 'attributes_assetsInstalledDate',
value: '2024-07-02T00:00:00.000Z',
},
{
attributeCode: 'attributes_wasteContainersAssisted',
value: true,
},
{
attributeCode: 'attributes_defaultTeamsDefaultTeam',
value: ['668e57ca7d187f0cc9e4ae68'],
},
],
parents: {
attributes_assetGroupsAssets: ['5ec4ee02fd0019005633b0fa'],
},
// optionally lock the item to prevent editing until unlocked
locked: false,
// optionally specify the context if you are a Causeway employee
context: 'Customer',
// optionally specify the new item's ID if you are a Causeway employee
// must be a unique 24-character alphanumeric code
itemId: '5f6b9c0e8163400065eae35f',
},
})
.then((response) => {
// the response contains the new item
const item = response.data.item;
// output the item's title to the console
console.log(`item "${item.itemId}" created!`);
// output each of the item's attributes to the console
item.attributes.forEach((a) => {
console.log(`attribute "${a.attributeCode}" = ${a.value}`);
});
})
.catch((error) => {
// output any error data to the console
console.log(error.response.data);
});