Question

Composing and making batch API calls from CSV. How would you approach this?

  • 4 December 2023
  • 2 replies
  • 138 views

Userlevel 2
Badge +1

THE PROBLEM: I have hundreds of shipments in my e-comm to update with tracking numbers. There is no batch upload process to do this but I can and have made API calls to update the shipments. Thing is, in order to update the shipment I need a ShipmentId which is a unique identifier and in order to get that I need to make another API call.

Here’s a zap I have in place that works fine:
 

I send a CSV with columns for ACCOUNT NUMBER, ORDER NUMBER, VENDOR NAME, and TRACKING NUMBER to Zapier. Import the CSV and then start to loop over them. First I make an API call using the ORDER NUMBER and VENDOR NAME – this returns the ShipmentId which I use to make another call to update the shipment with tracking number.

The ACCOUNT NUMBER is used for auth.

This all works fine, but you can imagine that this gobbles up a lot of tasks. I think there might be a better way, but I’m having a hard time wrapping my head around it.

THE POTENTIAL SOLUTION: 

My first thought was that I could use a code step to combine the two API calls (GET ShipmentId and PATCH ShipmentId) into one step. Simple enough thanks to Zapier’s AI assistant for code generation. That saves me one task per row processed. But! –

This e-comm platform supports batch API calls. Here’s how that works:

I can compose multiple calls into a JSON array and send that to one endpoint. Pretty cool! I even generated some code with Zapier to compose an array of calls to fetch the ShipmentIds in one step instead of however many rows are in my CSV. My thinking was I could get those ShipmentIds in a batch call like this, then have another code step that takes the returned ShipmentIds and composes a new JSON array including those ShipmentIds and the tracking numbers. Potentially updating all of the shipments with a few steps instead of potentially hundred.

It’s at this point that my brain begins to get a little scrambled:

  1. The batch API call can only have up to 100 individual calls in its JSON array. How would I build logic that does this in multiple parts for every 100 rows in the sheet?
  2. It seems like I would want to make my batch call to get the ShipmentIds and then using both the original CSV and the returned ShipmentIds, compose my call to update the shipments. Should I do that in a Table by Zapier, Storage, or is there a better way?

I’m not looking for code here. Really just curious to know how you would approach the project from a high level. Am I asking Zapier to do too much here?


This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

2 replies

Userlevel 7
Badge +14

Hi @ericg 

Good question.

Have you tried asking ChatGPT for help with configuring the custom Code logic?

 

You can do looping within the Code step for every 100.

 

Also, think about this...if one of the line items has an issue (e.g. invalid value or not match found), then it may impact the entire Zap Run / step from processing.

 

Keep in mind is that Code steps have a timeout limit and a memory limit.

 

Userlevel 2
Badge +1

Hey @Troy Tessalone  –

I’ve used ChatGPT to successfully write some code that will convert the CSV into a JSON array that works to send to the batch API endpoint in order to get Shipment IDs for each row. That worked pretty easily!

I have not asked ChatGPT about the best approach or order of operations to take with this project. Might be an interesting conversation to have.