With the array data type, you can make your automations more powerful and cut repetitive nodes in them without impacting their functionality.
In this article, you will learn how to create and modify arrays and get some real use cases. Note that arrays aren't available in automation actions and can only be used through API. This article will cover:
Creating arrays
Manychat arrays can contain texts and numbers. There are two ways how to create arrays:
- UI - In-product
- API
To create an array via UI, follow these steps:
Navigate to Settings → Fields:
Click + New User Field button. Add the field name and select Array from the dropdown list, then click Create.
Currently, it’s not possible to create a bot field with array type. Only user fields are supported.
To create an array via API, use the endpoint /fb/page/createCustomField and specify array as a type in the request body:
{
"caption": "My array",
"type": "array",
"description": "This field can store an array"
}
Reading arrays
Custom User Fields (CUFs) with array type are accessible via:
- UI - In-product
- API
To get the list of already existing CUFs, navigate to Settings → Fields. All CUFs are shown in a table format. You can check the type of a CUF in the column "Type".
You can retrieve the same list using the endpoint /fb/page/getCustomFields.
Each CUF object contains type:
CUF values associated with a particular contact are stored in a contact card. By default, Manychat shows the number of elements in an array field, but you can access all array elements by clicking on it. Here is an example:
The equivalent API endpoint is /fb/subscriber/getInfo.
The response contains all CUFs with their type and value:
Modifying arrays
Modifying data in CUFs with array type is possible via:
- External request mapping
- Application action mapping
- API
To map an array, you specify the JSON path to it, and Manychat saves this array into a CUF. Below, you can find an example based on a real service/API.
This is a JSON response from Shopify Order API. Each order is unique and may contain a different number of items. With arrays, you can save all Stock Keeping Units (SKUs) into 1 CUF. There is no need to parse and prepare a response for storing each SKU within its own CUF.
The API endpoints /fb/subscriber/setCustomField and /fb/subscriber/setCustomFields can be used for saving arrays as well. Below, you can find a few examples of the request body.
- set non-empty value
{
"subscriber_id": {{subscriber_id}},
"field_id": {{field_id}},
"field_value": [1, 2, 3, "apples"]
}
- set empty value
{
"subscriber_id": {{subscriber_id}},
"field_id": {{field_id}},
"field_value": []
}
- clear array field
{
"subscriber_id": {{subscriber_id}},
"field_id": {{field_id}},
"field_value": null
}
It’s not possible to modify or clear an array field in the UI.
Use cases
Arrays can be used for multiple use cases. What unites them all is that arrays are great when the data you work with contains an unknown number of elements. For example:
- E-commerce. Every cart and order is unique. Orders may contain any number of items. You can assume that an average order contains 2 or 3 items and map each item individually, but in this case, you can lose some data if your customer orders more items than you foreseen in your mapping.
- NLU/NLP based bots. Services like wit.ai analyze user input and send back intents and entities. Again, each user input is unique. arrays work perfectly no matter how many entities the service sends back.
- Multiple-choice questions. If you use forms/surveys to qualify your contacts, you might be familiar with multiple-choice questions. The number of options each contact can select varies. It's easier to store them in arrays rather than parse data and create a separate CUF for each answer.