The Neo4j transactional HTTP endpoint allows you to execute a series of Cypher statements within the scope of a transaction. The transaction may be kept open across multiple HTTP requests, until the client chooses to commit or roll back. Each HTTP request can include a list of statements, and for convenience you can include statements along with a request to begin or commit a transaction.
The server guards against orphaned transactions by using a timeout. If there are no requests for a given transaction within the timeout period, the server will roll it back. You can configure the timeout in the server configuration, by setting org.neo4j.server.transaction.timeout to the number of seconds before timeout. The default timeout is 60 seconds.
The key difference between the transactional HTTP endpoint and the cypher endpoint is the ability to use the same transaction across multiple HTTP requests. The cypher endpoint always attempts to commit a transaction at the end of each HTTP request.
![]() | Note |
---|---|
This is a new feature in the API, and is still experimental. Expect this API to change without notice, until this note is removed. |
![]() | Note |
---|---|
The serialization format for cypher results is mostly the same as the cypher endpoint. However, the format for raw entities is slightly less verbose and does not include hypermedia links. |
![]() | Note |
---|---|
Open transactions are not shared among members of an HA cluster. Therefore, if you use this endpoint in an HA cluster, you must ensure that all requests for a given transaction are sent to the same Neo4j instance. |
![]() | Tip |
---|---|
In order to speed up queries in repeated scenarios, try not to use literals but replace them with parameters wherever possible in order to let the server cache query plans. |
You begin a new transaction by posting zero or more cypher statements to the transaction endpoint. The server will respond with the result of your statements, as well as the location of your open transaction.
Example request
POST
http://localhost:7474/db/data/transaction
Accept:
application/json
Content-Type:
application/json
{ "statements" : [ { "statement" : "CREATE (n {props}) RETURN n", "parameters" : { "props" : { "name" : "My Node" } } } ] }
Example response
201:
Created
Content-Type:
application/json
Location:
http://localhost:7474/db/data/transaction/4
{ "commit" : "http://localhost:7474/db/data/transaction/4/commit", "results" : [ { "columns" : [ "n" ], "data" : [ [ { "name" : "My Node" } ] ] } ], "errors" : [ ] }
Given that you have an open transaction, you can make a number of requests, each of which executes additional statements, and keeps the transaction open.
Example request
POST
http://localhost:7474/db/data/transaction/6
Accept:
application/json
Content-Type:
application/json
{ "statements" : [ { "statement" : "CREATE n RETURN n" } ] }
Example response
200:
OK
Content-Type:
application/json
{ "commit" : "http://localhost:7474/db/data/transaction/6/commit", "results" : [ { "columns" : [ "n" ], "data" : [ [ { } ] ] } ], "errors" : [ ] }
Given you have an open transaction, you can send a commit request. Optionally, you submit additional statements along with the request that will be executed before committing the transaction.
Example request
POST
http://localhost:7474/db/data/transaction/2/commit
Accept:
application/json
Content-Type:
application/json
{ "statements" : [ { "statement" : "CREATE n RETURN id(n)" } ] }
Example response
200:
OK
Content-Type:
application/json
{ "results" : [ { "columns" : [ "id(n)" ], "data" : [ [ 2 ] ] } ], "errors" : [ ] }
Given that you have an open transaction, you can send a roll back request. The server will roll back the transaction.
Example request
DELETE
http://localhost:7474/db/data/transaction/1
Accept:
application/json
Example response
200:
OK
Content-Type:
application/json
{ "results" : [ ], "errors" : [ ] }
If there is no need to keep a transaction open across multiple HTTP requests, you can begin a transaction, execute statements, and commit with just a single HTTP request.
Example request
POST
http://localhost:7474/db/data/transaction/commit
Accept:
application/json
Content-Type:
application/json
{ "statements" : [ { "statement" : "CREATE n RETURN id(n)" } ] }
Example response
200:
OK
Content-Type:
application/json
{ "results" : [ { "columns" : [ "id(n)" ], "data" : [ [ 3 ] ] } ], "errors" : [ ] }
The result of any request against the transaction endpoint is streamed back to the client. Therefore the server does not know whether the request will be successful or not when it sends the HTTP status code.
Because of this, all requests against the transactional endpoint will return 200 or 201 status code, regardless of whether statements were successfully executed. At the end of the response payload, the server includes a list of errors that occurred while executing statements. If this list is empty, the request completed successfully.
If any errors occur while executing statements, the server will roll back the transaction.
In this example, we send the server an invalid statement to demonstrate error handling.
Example request
POST
http://localhost:7474/db/data/transaction/5/commit
Accept:
application/json
Content-Type:
application/json
{ "statements" : [ { "statement" : "This is not a valid Cypher Statement." } ] }
Example response
200:
OK
Content-Type:
application/json
{ "results" : [ ], "errors" : [ { "code" : 42001, "status" : "STATEMENT_SYNTAX_ERROR", "message" : "Syntax error in statement. Cause: invalid start of query\n\"This is not a valid Cypher Statement.\"\n ^" } ] }
Copyright © 2013 Neo Technology