# Method: projects.instances.databases.sessions.batchWrite

- [HTTP request](https://docs.cloud.google.com/spanner/docs/reference/rest/v1/projects.instances.databases.sessions/batchWrite#body.HTTP_TEMPLATE)
- [Path parameters](https://docs.cloud.google.com/spanner/docs/reference/rest/v1/projects.instances.databases.sessions/batchWrite#body.PATH_PARAMETERS)
- [Request body](https://docs.cloud.google.com/spanner/docs/reference/rest/v1/projects.instances.databases.sessions/batchWrite#body.request_body)
  - [JSON representation](https://docs.cloud.google.com/spanner/docs/reference/rest/v1/projects.instances.databases.sessions/batchWrite#body.request_body.SCHEMA_REPRESENTATION)
- [Response body](https://docs.cloud.google.com/spanner/docs/reference/rest/v1/projects.instances.databases.sessions/batchWrite#body.response_body)
  - [JSON representation](https://docs.cloud.google.com/spanner/docs/reference/rest/v1/projects.instances.databases.sessions/batchWrite#body.BatchWriteResponse.SCHEMA_REPRESENTATION)
- [Authorization scopes](https://docs.cloud.google.com/spanner/docs/reference/rest/v1/projects.instances.databases.sessions/batchWrite#body.aspect)
- [MutationGroup](https://docs.cloud.google.com/spanner/docs/reference/rest/v1/projects.instances.databases.sessions/batchWrite#MutationGroup)
  - [JSON representation](https://docs.cloud.google.com/spanner/docs/reference/rest/v1/projects.instances.databases.sessions/batchWrite#MutationGroup.SCHEMA_REPRESENTATION)
- [Try it!](https://docs.cloud.google.com/spanner/docs/reference/rest/v1/projects.instances.databases.sessions/batchWrite#try-it)

Batches the supplied mutation groups in a collection of efficient transactions. All mutations in a group are committed atomically. However, mutations across groups can be committed non-atomically in an unspecified order and thus, they must be independent of each other. Partial failure is possible, that is, some groups might have been committed successfully, while some might have failed. The results of individual batches are streamed into the response as the batches are applied.

`sessions.batchWrite` requests are not replay protected, meaning that each mutation group can be applied more than once. Replays of non-idempotent mutations can have undesirable effects. For example, replays of an insert mutation can produce an already exists error or if you use generated or commit timestamp-based keys, it can result in additional rows being added to the mutation's table. We recommend structuring your mutation groups to be idempotent to avoid this issue.

### HTTP request

Choose a location:
<button value="global" default="">global</button> <button value="europe-west8">europe-west8</button> <button value="me-central2">me-central2</button> <button value="us-central1">us-central1</button> <button value="us-central2">us-central2</button> <button value="us-east1">us-east1</button> <button value="us-east4">us-east4</button> <button value="us-east5">us-east5</button> <button value="us-south1">us-south1</button> <button value="us-west1">us-west1</button> <button value="us-west2">us-west2</button> <button value="us-west3">us-west3</button> <button value="us-west4">us-west4</button> <button value="us-west8">us-west8</button> <button value="us-east7">us-east7</button>   
`POST https://spanner.googleapis.com/v1/{session=projects/*/instances/*/databases/*/sessions/*}:batchWrite`

<br />

The URLs use [gRPC Transcoding](https://google.aip.dev/127) syntax.

### Path parameters

| Parameters ||
|---|---|
| `session` | `string` Required. The session in which the batch request is to be run. Authorization requires the following [IAM](https://cloud.google.com/iam/docs/) permission on the specified resource `session`: - `spanner.databases.write` |

### Request body

The request body contains data with the following structure:

| JSON representation |
|---|
| ``` { "requestOptions": { object (`https://docs.cloud.google.com/spanner/docs/reference/rest/v1/RequestOptions`) }, "mutationGroups": [ { object (`https://docs.cloud.google.com/spanner/docs/reference/rest/v1/projects.instances.databases.sessions/batchWrite#MutationGroup`) } ], "excludeTxnFromChangeStreams": boolean } ``` |

| Fields ||
|---|---|
| `requestOptions` | ``object (`https://docs.cloud.google.com/spanner/docs/reference/rest/v1/RequestOptions`)`` Common options for this request. |
| `mutationGroups[]` | ``object (`https://docs.cloud.google.com/spanner/docs/reference/rest/v1/projects.instances.databases.sessions/batchWrite#MutationGroup`)`` Required. The groups of mutations to be applied. |
| `excludeTxnFromChangeStreams` | `boolean` Optional. If you don't set the `excludeTxnFromChangeStreams` option or if it's set to `false`, then any change streams monitoring columns modified by transactions will capture the updates made within that transaction. |

### Response body

The result of applying a batch of mutations.

If successful, the response body contains data with the following structure:

| JSON representation |
|---|
| ``` { "indexes": [ integer ], "status": { object (`https://docs.cloud.google.com/spanner/docs/reference/rest/v1/Status`) }, "commitTimestamp": string } ``` |

| Fields ||
|---|---|
| `indexes[]` | `integer` The mutation groups applied in this batch. The values index into the `mutationGroups` field in the corresponding `BatchWriteRequest`. |
| `status` | ``object (`https://docs.cloud.google.com/spanner/docs/reference/rest/v1/Status`)`` An `OK` status indicates success. Any other status indicates a failure. |
| `commitTimestamp` | ``string (`https://protobuf.dev/reference/protobuf/google.protobuf#timestamp` format)`` The commit timestamp of the transaction that applied this batch. Present if status is OK and the mutation groups were applied, absent otherwise. For mutation groups with conditions, a status=OK and missing commitTimestamp means that the mutation groups were not applied due to the condition not being satisfied after evaluation. Uses RFC 3339, where generated output will always be Z-normalized and use 0, 3, 6 or 9 fractional digits. Offsets other than "Z" are also accepted. Examples: `"2014-10-02T15:01:23Z"`, `"2014-10-02T15:01:23.045123456Z"` or `"2014-10-02T15:01:23+05:30"`. |

### Authorization scopes

Requires one of the following OAuth scopes:

- `https://www.googleapis.com/auth/spanner.data`
- `https://www.googleapis.com/auth/cloud-platform`

For more information, see the [Authentication Overview](https://docs.cloud.google.com/docs/authentication#authorization-gcp).

## MutationGroup

A group of mutations to be committed together. Related mutations should be placed in a group. For example, two mutations inserting rows with the same primary key prefix in both parent and child tables are related.

| JSON representation |
|---|
| ``` { "mutations": [ { object (`https://docs.cloud.google.com/spanner/docs/reference/rest/v1/Mutation`) } ] } ``` |

| Fields ||
|---|---|
| `mutations[]` | ``object (`https://docs.cloud.google.com/spanner/docs/reference/rest/v1/Mutation`)`` Required. The mutations in this group. |