Best answer

Creating an Action for an API endpoint that expects a list/array of objects using "Line Item Groups"

  • 23 February 2024
  • 2 replies
  • 95 views

Userlevel 1
Badge

Hi all,

I am working on a create action, but the API endpoint in question allows for multiple objects to be uploaded at once, so an array of objects. i.e., POST request body could contain:

[
{
"email": "test1@exmaple.com",
"firstName": "Test1",
"lastName": "User1"
},
{
"email": "{{studentEmail12}}",
"firstName": "Test2",
"lastName": "User2"
}
]

and the response to a successful post would then be:

[
{
"id":"PT5wQ_avusMWVymA7aMRZwUw2",
"firstName":"Test1",
"lastName":"User1",
"email":"test1@exmaple.com",
"role":"student",
"invitationStatus":"notSent",
"participationStatus":"pending",
"firstJoined":null
},
{
"id":"PT_gi07CDBHwwmM0wkVtpbnQ2",
"firstName":"Test2",
"lastName":"User2",
"email":"test2@example.com",
"role":"student",
"invitationStatus":
"notSent","participationStatus":
"pending","firstJoined":null
}
]

Zapier actions require that an object is defined both in the request and response, so natively, the above will fail. One way for working around this is to limit the API request to only send a single object. I can then modify the body in the code view to be an array (by adding square braces), and ensure the `results` returned are wrapped in curly braces to transform it into an object. such as :

const options = {
url: `https://api.accelerate.cloudshare.com/v4/trainings/${bundle.inputData.training_id}/students`,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': `Bearer ${bundle.authData.access_token}`
},
params: {

},
body: [
{
'email': bundle.inputData.email,
'firstName': bundle.inputData.firstName,
'lastName': bundle.inputData.lastName
}
]
}

return z.request(options)
.then((response) => {
response.throwForStatus();
const results = response.json;

// You can do any parsing you need for results here before returning them

return {results};
});

This works, but kind of defeats the ability of the API endpoint as now multiple requests have to be made to create multiple students, which is inefficient. 

I suspect that “Line Item Groups” can be used, but I am struggling to understand how these can be used and referenced within the “Configure your API request” and the “Test your API” steps within the API configuration. 

 

Any help would be appreciated.

icon

Best answer by swinster 26 February 2024, 15:33

View original

2 replies

Userlevel 1
Badge

OK, so I have had some back and forth with Zapier support, who have been most helpful. 

To achieve this, the action item contains a ingle line item group (Students), which in run contain the fields required for each Student. 

 

This would give a parameter key with the name `Students` with the value as an array of objects, similar to:

"students": [
{
"firstName": "Test1",
"lastName": "User1",
"email":"test1@example.com"
},
{
"firstName": "Test2",
"lastName": "User2",
"email":"test2@example.com"
}
]

However, the API requires just the array of objects (not the parameter). The array portion is referenced in the bundle as:

  • bundle.inputData.students

 

So, the API Configuration requires Code View, and I can override the body to reference this bundle.inputData.students, thus:

 

To Test the API request, I could then update the RAW view of the test JSON to be:

 

Which gives a successful response:

 

 

There are some additional things to think about, so I will follow up with those shortly

Userlevel 1
Badge

Zapier support pointed out that their users might expect that actions typically to take and return one object within the body of the request/response. So, providing an action that goes against this culture might cause some headaches for users, and they provided some great additional understanding that is worthwhile re-posting:

I mention this because it's important to have a good understanding of how Zapier users will be expecting to use actions that have support for line items, such as the "Add Students" action you're building here. The main points to note when building actions that have support for line items are:
 

  1. Users will need to supply line item as input data to your action, either from a trigger or preceding action that supplies line item data, or by creating them.
  2. Users will expect to be able to use all the new items created by your action in other actions in their Zaps. For example, after creating new students in your app, they may also want to add all those new students to a Google Sheet (as individual rows) or perhaps use a Looping by Zapier action to send an email to each student.

 

Whilst I accept this PoV, I also feel that our users would typically expect to be able to interact with the API as it has been defined and implemented. However, from a pragmatic PoV, I wonder if it is in the interests of the user to provide two actions for this endpoint, so users could:

  1. Supply data in the “line item” format as seen above (thus an array of objects),
  2. Supply data as individual parameters (which would need to be converted into an array of objects for the API to work, but essentially would only ever send one record.

Reply