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: d
{
'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.