21.9. Relationships

Relationships are a first class citizen in the Neo4j REST API. They can be accessed either stand-alone or through the nodes they are attached to.

The general pattern to get relationships from a node is:

GET http://localhost:7474/db/data/node/123/relationships/{dir}/{-list|&|types}

Where dir is one of all, in, out and types is an ampersand-separated list of types. See the examples below for more information.

Get Relationship by ID

Figure 21.19. Final Graph

Example request

  • GET http://localhost:7474/db/data/relationship/13
  • Accept: application/json; charset=UTF-8

Example response

  • 200: OK
  • Content-Type: application/json; charset=UTF-8
{
  "extensions" : { },
  "start" : "http://localhost:7474/db/data/node/32",
  "property" : "http://localhost:7474/db/data/relationship/13/properties/{key}",
  "self" : "http://localhost:7474/db/data/relationship/13",
  "properties" : "http://localhost:7474/db/data/relationship/13/properties",
  "type" : "know",
  "end" : "http://localhost:7474/db/data/node/31",
  "metadata" : {
    "id" : 13,
    "type" : "know"
  },
  "data" : { }
}

Create relationship

Upon successful creation of a relationship, the new relationship is returned.

Figure 21.20. Final Graph

Example request

  • POST http://localhost:7474/db/data/node/1/relationships
  • Accept: application/json; charset=UTF-8
  • Content-Type: application/json
{
  "to" : "http://localhost:7474/db/data/node/0",
  "type" : "LOVES"
}

Example response

  • 201: Created
  • Content-Type: application/json; charset=UTF-8
  • Location: http://localhost:7474/db/data/relationship/1
{
  "extensions" : { },
  "start" : "http://localhost:7474/db/data/node/1",
  "property" : "http://localhost:7474/db/data/relationship/1/properties/{key}",
  "self" : "http://localhost:7474/db/data/relationship/1",
  "properties" : "http://localhost:7474/db/data/relationship/1/properties",
  "type" : "LOVES",
  "end" : "http://localhost:7474/db/data/node/0",
  "metadata" : {
    "id" : 1,
    "type" : "LOVES"
  },
  "data" : { }
}

Create a relationship with properties

Upon successful creation of a relationship, the new relationship is returned.

Figure 21.21. Starting Graph

Figure 21.22. Final Graph

Example request

  • POST http://localhost:7474/db/data/node/11/relationships
  • Accept: application/json; charset=UTF-8
  • Content-Type: application/json
{
  "to" : "http://localhost:7474/db/data/node/10",
  "type" : "LOVES",
  "data" : {
    "foo" : "bar"
  }
}

Example response

  • 201: Created
  • Content-Type: application/json; charset=UTF-8
  • Location: http://localhost:7474/db/data/relationship/8
{
  "extensions" : { },
  "start" : "http://localhost:7474/db/data/node/11",
  "property" : "http://localhost:7474/db/data/relationship/8/properties/{key}",
  "self" : "http://localhost:7474/db/data/relationship/8",
  "properties" : "http://localhost:7474/db/data/relationship/8/properties",
  "type" : "LOVES",
  "end" : "http://localhost:7474/db/data/node/10",
  "metadata" : {
    "id" : 8,
    "type" : "LOVES"
  },
  "data" : {
    "foo" : "bar"
  }
}

Delete relationship

Figure 21.23. Starting Graph

Figure 21.24. Final Graph

Example request

  • DELETE http://localhost:7474/db/data/relationship/6
  • Accept: application/json; charset=UTF-8

Example response

  • 204: No Content

Get all properties on a relationship

Figure 21.25. Final Graph

Example request

  • GET http://localhost:7474/db/data/relationship/9/properties
  • Accept: application/json; charset=UTF-8

Example response

  • 200: OK
  • Content-Type: application/json; charset=UTF-8
{
  "cost" : "high",
  "since" : "1day"
}

Set all properties on a relationship

Figure 21.26. Starting Graph

Figure 21.27. Final Graph

Example request

  • PUT http://localhost:7474/db/data/relationship/14/properties
  • Accept: application/json; charset=UTF-8
  • Content-Type: application/json
{
  "happy" : false
}

Example response

  • 204: No Content

Get single property on a relationship

Figure 21.28. Final Graph

Example request

  • GET http://localhost:7474/db/data/relationship/10/properties/cost
  • Accept: application/json; charset=UTF-8

Example response

  • 200: OK
  • Content-Type: application/json; charset=UTF-8
"high"

Set single property on a relationship

Figure 21.29. Starting Graph

Figure 21.30. Final Graph

Example request

  • PUT http://localhost:7474/db/data/relationship/8/properties/cost
  • Accept: application/json; charset=UTF-8
  • Content-Type: application/json
"deadly"

Example response

  • 204: No Content

Get all relationships

Figure 21.31. Final Graph

Example request

  • GET http://localhost:7474/db/data/node/328/relationships/all
  • Accept: application/json; charset=UTF-8

Example response

  • 200: OK
  • Content-Type: application/json; charset=UTF-8
[ {
  "start" : "http://localhost:7474/db/data/node/328",
  "data" : { },
  "self" : "http://localhost:7474/db/data/relationship/203",
  "property" : "http://localhost:7474/db/data/relationship/203/properties/{key}",
  "properties" : "http://localhost:7474/db/data/relationship/203/properties",
  "type" : "LIKES",
  "extensions" : { },
  "end" : "http://localhost:7474/db/data/node/329",
  "metadata" : {
    "id" : 203,
    "type" : "LIKES"
  }
}, {
  "start" : "http://localhost:7474/db/data/node/330",
  "data" : { },
  "self" : "http://localhost:7474/db/data/relationship/204",
  "property" : "http://localhost:7474/db/data/relationship/204/properties/{key}",
  "properties" : "http://localhost:7474/db/data/relationship/204/properties",
  "type" : "LIKES",
  "extensions" : { },
  "end" : "http://localhost:7474/db/data/node/328",
  "metadata" : {
    "id" : 204,
    "type" : "LIKES"
  }
}, {
  "start" : "http://localhost:7474/db/data/node/328",
  "data" : { },
  "self" : "http://localhost:7474/db/data/relationship/205",
  "property" : "http://localhost:7474/db/data/relationship/205/properties/{key}",
  "properties" : "http://localhost:7474/db/data/relationship/205/properties",
  "type" : "HATES",
  "extensions" : { },
  "end" : "http://localhost:7474/db/data/node/331",
  "metadata" : {
    "id" : 205,
    "type" : "HATES"
  }
} ]

Get incoming relationships

Figure 21.32. Final Graph

Example request

  • GET http://localhost:7474/db/data/node/347/relationships/in
  • Accept: application/json; charset=UTF-8

Example response

  • 200: OK
  • Content-Type: application/json; charset=UTF-8
[ {
  "start" : "http://localhost:7474/db/data/node/349",
  "data" : { },
  "self" : "http://localhost:7474/db/data/relationship/215",
  "property" : "http://localhost:7474/db/data/relationship/215/properties/{key}",
  "properties" : "http://localhost:7474/db/data/relationship/215/properties",
  "type" : "LIKES",
  "extensions" : { },
  "end" : "http://localhost:7474/db/data/node/347",
  "metadata" : {
    "id" : 215,
    "type" : "LIKES"
  }
} ]

Get outgoing relationships

Figure 21.33. Final Graph

Example request

  • GET http://localhost:7474/db/data/node/372/relationships/out
  • Accept: application/json; charset=UTF-8

Example response

  • 200: OK
  • Content-Type: application/json; charset=UTF-8
[ {
  "start" : "http://localhost:7474/db/data/node/372",
  "data" : { },
  "self" : "http://localhost:7474/db/data/relationship/229",
  "property" : "http://localhost:7474/db/data/relationship/229/properties/{key}",
  "properties" : "http://localhost:7474/db/data/relationship/229/properties",
  "type" : "LIKES",
  "extensions" : { },
  "end" : "http://localhost:7474/db/data/node/373",
  "metadata" : {
    "id" : 229,
    "type" : "LIKES"
  }
}, {
  "start" : "http://localhost:7474/db/data/node/372",
  "data" : { },
  "self" : "http://localhost:7474/db/data/relationship/231",
  "property" : "http://localhost:7474/db/data/relationship/231/properties/{key}",
  "properties" : "http://localhost:7474/db/data/relationship/231/properties",
  "type" : "HATES",
  "extensions" : { },
  "end" : "http://localhost:7474/db/data/node/375",
  "metadata" : {
    "id" : 231,
    "type" : "HATES"
  }
} ]

Get typed relationships

Note that the "&" needs to be encoded like "%26" for example when using cURL from the terminal.

Figure 21.34. Final Graph

Example request

  • GET http://localhost:7474/db/data/node/303/relationships/all/LIKES&HATES
  • Accept: application/json; charset=UTF-8

Example response

  • 200: OK
  • Content-Type: application/json; charset=UTF-8
[ {
  "start" : "http://localhost:7474/db/data/node/303",
  "data" : { },
  "self" : "http://localhost:7474/db/data/relationship/188",
  "property" : "http://localhost:7474/db/data/relationship/188/properties/{key}",
  "properties" : "http://localhost:7474/db/data/relationship/188/properties",
  "type" : "LIKES",
  "extensions" : { },
  "end" : "http://localhost:7474/db/data/node/304",
  "metadata" : {
    "id" : 188,
    "type" : "LIKES"
  }
}, {
  "start" : "http://localhost:7474/db/data/node/305",
  "data" : { },
  "self" : "http://localhost:7474/db/data/relationship/189",
  "property" : "http://localhost:7474/db/data/relationship/189/properties/{key}",
  "properties" : "http://localhost:7474/db/data/relationship/189/properties",
  "type" : "LIKES",
  "extensions" : { },
  "end" : "http://localhost:7474/db/data/node/303",
  "metadata" : {
    "id" : 189,
    "type" : "LIKES"
  }
}, {
  "start" : "http://localhost:7474/db/data/node/303",
  "data" : { },
  "self" : "http://localhost:7474/db/data/relationship/190",
  "property" : "http://localhost:7474/db/data/relationship/190/properties/{key}",
  "properties" : "http://localhost:7474/db/data/relationship/190/properties",
  "type" : "HATES",
  "extensions" : { },
  "end" : "http://localhost:7474/db/data/node/306",
  "metadata" : {
    "id" : 190,
    "type" : "HATES"
  }
} ]

Get relationships on a node without relationships

Figure 21.35. Final Graph

Example request

  • GET http://localhost:7474/db/data/node/361/relationships/all
  • Accept: application/json; charset=UTF-8

Example response

  • 200: OK
  • Content-Type: application/json; charset=UTF-8
[ ]