Skip to content

Batch Requests

Arun Prakash edited this page Oct 25, 2026 · 1 revision

Batch Requests

client.batch sends several writes in a single HTTP call using the WordPress batch endpoint (/wp-json/batch/v1, WordPress 5.6+). It saves round trips when you create, update or delete many items at once.

final response = await client.batch.run(
  [
    const BatchOperation.create('posts', body: {'title': 'A', 'status': 'draft'}),
    BatchOperation.update('posts', 12, body: {'title': 'B'}),
    BatchOperation.delete('posts', 13, force: true),
    const BatchOperation(HttpMethod.post, '/my-plugin/v1/events', body: {'title': 'C'}),  // explicit route
  ],
  validation: BatchValidation.requireAllValidate,   // all-or-nothing validation
);

final result = response.dataOrThrow();
for (final item in result.responses) {
  print('${item.status}: ${item.body}');              // item.error is set on failure
}
print(result.succeeded.length);
print(result.failures.map((f) => f.error?.code));
final created = result.responses.first.decode((json) => Post.fromJson(json));

An individual operation failing does not fail the call: check succeeded / failures. The call itself fails only on transport or authorization errors.

Rules of the endpoint

  • Writes only (POST, PUT, PATCH, DELETE). Reads cannot be batched.
  • Only routes that opted in accept batching. Core routes such as posts, pages and media do; a plugin route must declare allow_batch in its route registration. A route that did not opt in answers that item with an error.
  • At most 25 requests per call by default. chunkSize (default 25) splits longer lists into several calls that run one after another; results are concatenated in order. Raise it only if a plugin raised the server limit.
  • A path starting with / is used as is, anything else is relative to /wp/v2/: BatchOperation(HttpMethod.post, '/my-plugin/v1/events', body: {...}).
  • BatchValidation.normal (default) runs each item on its own; requireAllValidate validates everything first and runs nothing if any item is invalid.
  • Authorization is the client's default credentials, as for any write (Authorization).

Clone this wiki locally