Search code examples
json-api

What is the correct JSONAPI way to post multiple related entities in a single request?


At some point in my hypothetical app, I want to create multiple related entities of different types in a single request, for efficiency sake. In the example below I serialize the request in a way that it contains the data about the new User as well as its related Avatar.

// POST /api/users
{
    data: {
        attributes: { ... },
        type: 'user',
        relationships: {
            avatar: {
                data: {
                    attributes: { ... }
                    type: 'avatar',
                }
            }
        }
    }
}

The question is, what would be the correct/recommended way (if there's any) to do that in JSONAPI?


Solution

  • Creating or updating multiple resources in a single request is not supported by JSON:API spec yet. However there is a proposal for an Atomic Operations extension for the upcoming v1.1 of the spec.

    But in most cases such a feature is not required for efficiency. You might even cause more load to the server by bundling multiple create or update requests into one. Doing multiple requests in parallel is cheap with HTTP/2 nowadays.

    It might not be as performant as doing it with one requests if the operations depend on each other (e.g. must await a post to be created before a comment for this post could be created). But in that case atomic transactions are also a strong requirement. That's the main driver behind that extension.

    So to answer your question:

    • It's currently not supported in JSON:API spec.
    • There is a good chance that it will be supported in the next version (v1.1) by an extension.
    • If efficiency is the only reason you are looking for such a feature, you might not need it at all.