WordPress REST API Batch Requests: Send Multiple API Calls in One HTTP Request with /batch/v1

REST API batch requests: several changes in one HTTP call

WordPress 5.6 added a batch endpoint to the REST API, /wp-json/batch/v1. It accepts a list of requests in one HTTP call and runs them one after the other on the server. That saves round trips when a screen saves many things at once, for example the block-based widget editor in 5.8, which saves all widgets with it. There are two limits to know before using it: it accepts only write requests, and only routes that allow batching.

Problem: An admin screen updates the status of twenty posts at once, and twenty separate REST requests make it slow and leave the data half-saved when one of them fails.

Solution: Send one POST request to /batch/v1 with a requests array of up to 25 requests (POST, PUT, PATCH or DELETE). Use "validation": "require-all-validate" to run none of them if one is invalid. For your own routes, enable batching with allow_batch.

A batch of updates

async function publishPosts( ids ) {
    const response = await fetch( '/wp-json/batch/v1', {
        method: 'POST',
        credentials: 'same-origin',
        headers: { 'Content-Type': 'application/json', 'X-WP-Nonce': haSettings.nonce },
        body: JSON.stringify( {
            validation: 'require-all-validate',
            requests: ids.map( ( id ) => ( {
                method: 'POST',
                path: '/wp/v2/posts/' + id,
                body: { status: 'publish' }
            } ) )
        } )
    } );

    const result = await response.json();
    // result.responses: one entry per request, in the same order,
    // each with status, headers and body like a normal REST response
    return result.responses.filter( ( r ) => r.status >= 400 );
}

The nonce comes from wp_create_nonce( 'wp_rest' ), passed to the script with wp_localize_script(). Each request is checked with its route’s own permission_callback, exactly as if it were sent alone, so batching gives nobody extra rights.

With require-all-validate, WordPress first validates the arguments of all requests and runs none of them if one is invalid; without it, the valid ones run and the invalid ones return their errors. It is not a database transaction: once the requests run, a failing one does not roll back the ones before it.

Your own routes

Core enables batching for posts, pages, terms, widgets and a few other routes. Custom routes opt in:

<?php
add_action( 'rest_api_init', function () {
    register_rest_route( 'ha/v1', '/items/(?P<id>\d+)', array(
        'methods'             => 'POST',
        'callback'            => 'ha_update_item',
        'permission_callback' => function () {
            return current_user_can( 'edit_posts' );
        },
        'allow_batch'         => array( 'v1' => true ),
    ) );
} );

A request to a route without allow_batch is answered inside the batch with an error for that item.

What about reading

GET requests cannot be batched. For a screen that needs posts, users and categories at once, the options are: send the GET requests in parallel with Promise.all() (HTTP/2 makes that cheap), use _embed to include linked objects in one response, or write one custom route that returns exactly the data the screen needs. The last one is often the fastest, because it runs a single PHP request on the server.

NOTE: A batch is limited to 25 requests by default (the rest_get_max_batch_size filter changes it). Split longer lists into several batches, and show the user which items failed instead of a single “error” for the whole batch.