While coming up with a solution design for an API integration piece for work I found the REST API batch resource feature relevant to my use case. The feature has been around since Summer 2015 and I didn’t know much about it then. Basically the batch resource allows you to make multiple request in a single API call.
I needed to come up with a simple solution for creating a single record and has option to pass an array of records to be created. I initially had been looking at 3 options and listed the things to consider on the development side.
- REST API via the sobject resource
- use sobject resource for record creation
- no Apex code
- cannot do array
- Apex Rest API
- accepts an array of the records
- use of JSON parser and deserializing the request body
- create test classes
- Bulk API
- last option
- overkill for the job and too many things to consider to get the integration setup
This is where batch resource came to the rescue. With batch resource you can do maximum of 25 subrequest and it supports the sobject resource I listed on the first option. Note though that subrequests are independent, and you can’t pass information between them.
As of this writing batching is supported on the following resource with API version 34.0 and higher.
- SObject resources—vXX.X/sobjects/
- Connect resources—vXX.X/connect/
- Chatter resources—vXX.X/chatter/
To use simply call and POST to the URI vXX.x/composite/batch
Sample JSON body
So before considering Apex Rest check if batch resource can solve your problem.