[[rest-api-execute-multiple-operations-in-batch]]
=== Execute multiple operations in batch ===

This lets you execute multiple API calls through a single HTTP call,
significantly improving performance for large insert and update
operations.

The batch service expects an array of job descriptions as input, each job
description describing an action to be performed via the normal server
API.

This service is transactional. If any of the operations performed fails
(returns a non-2xx HTTP status code), the transaction will be rolled back
and all changes will be undone.

Each job description should contain a +to+ attribute, with a value
relative to the data API root (so http://localhost:7474/db/data/node becomes
just /node), and a +method+ attribute containing HTTP verb to use.

Optionally you may provide a +body+ attribute, and an +id+ attribute to
help you keep track of responses, although responses are guaranteed to be
returned in the same order the job descriptions are received.

The following figure outlines the different parts of the job
descriptions:

image::batch-request-api.png[]


.Final Graph
["dot", "Final-Graph-Execute-multiple-operations-in-batch.svg", "neoviz"]
----
  N45 [
    label = "{Node\[45\]|name = \'John\'\l}"
  ]
  N46 [
    label = "{Node\[46\]|age = 1\l}"
  ]
  N46 -> N45 [
    color = "#2e3436"
    fontcolor = "#2e3436"
    label = "knows\n"
  ]
  N47 [
    label = "{Node\[47\]|age = 1\l}"
  ]
  N48 [
    label = "{Node\[48\]|age = 1\l}"
  ]
----

_Example request_

* *+POST+*  +http://localhost:7474/db/data/batch+
* *+Accept:+* +application/json+
* *+Content-Type:+* +application/json+
[source,javascript]
----
[ {
  "method" : "PUT",
  "to" : "/node/46/properties",
  "body" : {
    "age" : 1
  },
  "id" : 0
}, {
  "method" : "GET",
  "to" : "/node/46",
  "id" : 1
}, {
  "method" : "POST",
  "to" : "/node",
  "body" : {
    "age" : 1
  },
  "id" : 2
}, {
  "method" : "POST",
  "to" : "/node",
  "body" : {
    "age" : 1
  },
  "id" : 3
} ]
----


_Example response_

* *+200:+* +OK+
* *+Content-Type:+* +application/json+
[source,javascript]
----
[ {
  "id" : 0,
  "from" : "/node/46/properties"
}, {
  "id" : 1,
  "body" : {
    "extensions" : {
    },
    "paged_traverse" : "http://localhost:7474/db/data/node/46/paged/traverse/{returnType}{?pageSize,leaseTime}",
    "outgoing_relationships" : "http://localhost:7474/db/data/node/46/relationships/out",
    "traverse" : "http://localhost:7474/db/data/node/46/traverse/{returnType}",
    "all_typed_relationships" : "http://localhost:7474/db/data/node/46/relationships/all/{-list|&|types}",
    "all_relationships" : "http://localhost:7474/db/data/node/46/relationships/all",
    "property" : "http://localhost:7474/db/data/node/46/properties/{key}",
    "self" : "http://localhost:7474/db/data/node/46",
    "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/46/relationships/out/{-list|&|types}",
    "properties" : "http://localhost:7474/db/data/node/46/properties",
    "incoming_relationships" : "http://localhost:7474/db/data/node/46/relationships/in",
    "incoming_typed_relationships" : "http://localhost:7474/db/data/node/46/relationships/in/{-list|&|types}",
    "create_relationship" : "http://localhost:7474/db/data/node/46/relationships",
    "data" : {
      "age" : 1
    }
  },
  "from" : "/node/46"
}, {
  "id" : 2,
  "location" : "http://localhost:7474/db/data/node/47",
  "body" : {
    "extensions" : {
    },
    "paged_traverse" : "http://localhost:7474/db/data/node/47/paged/traverse/{returnType}{?pageSize,leaseTime}",
    "outgoing_relationships" : "http://localhost:7474/db/data/node/47/relationships/out",
    "traverse" : "http://localhost:7474/db/data/node/47/traverse/{returnType}",
    "all_typed_relationships" : "http://localhost:7474/db/data/node/47/relationships/all/{-list|&|types}",
    "all_relationships" : "http://localhost:7474/db/data/node/47/relationships/all",
    "property" : "http://localhost:7474/db/data/node/47/properties/{key}",
    "self" : "http://localhost:7474/db/data/node/47",
    "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/47/relationships/out/{-list|&|types}",
    "properties" : "http://localhost:7474/db/data/node/47/properties",
    "incoming_relationships" : "http://localhost:7474/db/data/node/47/relationships/in",
    "incoming_typed_relationships" : "http://localhost:7474/db/data/node/47/relationships/in/{-list|&|types}",
    "create_relationship" : "http://localhost:7474/db/data/node/47/relationships",
    "data" : {
      "age" : 1
    }
  },
  "from" : "/node"
}, {
  "id" : 3,
  "location" : "http://localhost:7474/db/data/node/48",
  "body" : {
    "extensions" : {
    },
    "paged_traverse" : "http://localhost:7474/db/data/node/48/paged/traverse/{returnType}{?pageSize,leaseTime}",
    "outgoing_relationships" : "http://localhost:7474/db/data/node/48/relationships/out",
    "traverse" : "http://localhost:7474/db/data/node/48/traverse/{returnType}",
    "all_typed_relationships" : "http://localhost:7474/db/data/node/48/relationships/all/{-list|&|types}",
    "all_relationships" : "http://localhost:7474/db/data/node/48/relationships/all",
    "property" : "http://localhost:7474/db/data/node/48/properties/{key}",
    "self" : "http://localhost:7474/db/data/node/48",
    "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/48/relationships/out/{-list|&|types}",
    "properties" : "http://localhost:7474/db/data/node/48/properties",
    "incoming_relationships" : "http://localhost:7474/db/data/node/48/relationships/in",
    "incoming_typed_relationships" : "http://localhost:7474/db/data/node/48/relationships/in/{-list|&|types}",
    "create_relationship" : "http://localhost:7474/db/data/node/48/relationships",
    "data" : {
      "age" : 1
    }
  },
  "from" : "/node"
} ]
----


