4.1. Get started

This section gives an overview of the official Neo4j drivers and how to connect to a Neo4j database with a "Hello World" example.

4.1.1. About the official drivers

Both official and community database drivers are available to provide access to Neo4j for applications. Official drivers exist for:

  • C# — works with any .NET language
  • Java — works with any JVM language
  • JavaScript
  • Python

The driver API is intended to be topologically agnostic. By this, we mean that the underlying database topology — single instance, causal cluster, etc. — can be altered without requiring a corresponding alteration to application code. In the general case, only the connection URI needs to be modified when changes are made to the topology.

The official drivers do not support HTTP communication. If you need an HTTP driver, there are a number of community drivers to choose from. See also Chapter 5, HTTP API.

4.1.2. Driver versions and installation

The 1.x series drivers have been built for Neo4j 3.x. The driver major version number correlates with the Bolt protocol version, the minor version number describes the driver feature set and the patch number defines the patch level of the driver in the regular sense. While minor versions of drivers will generally be released at the same time across languages, patch levels may vary.

It is recommended to always use the latest driver release within a major series. This will ensure that all server functionality is made available to client applications. To install a driver or to find out more about which driver versions are available, use the relevant language distribution system.

Example 4.1. Acquire the driver

To find out the latest version of the driver, visit https://www.nuget.org/packages/Neo4j.Driver/.

To install the latest version of the driver using NuGet in Visual Studio:

PM> Install-Package Neo4j.Driver

You can also choose to install a certain version of the driver.

Below is the syntax for installing a certain version of the driver.

PM> Install-Package Neo4j.Driver -Version $DOTNET_DRIVER_VERSION

In the following example we are installing driver version 1.5.2.

PM> Install-Package Neo4j.Driver -Version 1.5.2

You can review the release notes for this driver here.

To find out the latest version of the driver, visit the maven repository.

Example 4.2. Install the Java driver using Maven

When using Maven, add the following block to your pom.xml file. Note the placeholder for the driver version. You will have to find out the exact patch version that you wish to install.

<dependencies>
    <dependency>
        <groupId>org.neo4j.driver</groupId>
        <artifactId>neo4j-java-driver</artifactId>
        <version>$JAVA_DRIVER_VERSION</version>
    </dependency>
</dependencies>

In the following example we are adding driver version 1.5.0.

<dependencies>
    <dependency>
        <groupId>org.neo4j.driver</groupId>
        <artifactId>neo4j-java-driver</artifactId>
        <version>1.5.0</version>
    </dependency>
</dependencies>
Example 4.3. Add the dependency for the Java driver for Gradle or Grails

For Gradle or Grails, this is how to add the dependency. Again, note the placeholder for the driver version. You will have to find out the exact patch version that you wish to install.

compile 'org.neo4j.driver:neo4j-java-driver:$JAVA_DRIVER_VERSION'

In the following example we are adding driver version 1.5.0.

compile 'org.neo4j.driver:neo4j-java-driver:1.5.0'

You can review the release notes for this driver here.

Use npm to find out the latest version of the driver:

npm show neo4j-driver@* version

To install the latest version of the driver:

npm install neo4j-driver

You can also choose to install a certain version of the driver.

Below is the syntax for installing a certain version of the driver.

npm install neo4j-driver@$JAVASCRIPT-DRIVER-VERSION

In the following example we are installing driver version 1.5.2.

npm install neo4j-driver@1.5.2

You can review the release notes for this driver here.

To find out the latest version of the driver, visit https://pypi.python.org/pypi/neo4j-driver.

To install the latest version of the driver:

pip install neo4j-driver

You can also choose to install a certain version of the driver.

The following is the syntax for installing a certain version of the driver.

pip install neo4j-driver==$PYTHON_DRIVER_VERSION

In the following example we are installing driver version 1.5.3.

pip install neo4j-driver==1.5.3

You can review the release notes for this driver here.

4.1.3. Supported language and framework versions

For each language/framework, there is a set of versions that are officially supported by Neo4j.

Table 4.1. Supported languages/frameworks for the 1.x driver series
Language/framework Versions supported

Java

Oracle JDK 7/8 and OpenJDK 7/8 (latest patch releases)

Python

CPython 2.7, 3.4, 3.5 and 3.6 (latest patch releases)

JavaScript

our drivers are built to work with all LTS versions of Node.JS, specifically the 4.x and 6.x series runtimes (see https://github.com/nodejs/LTS).

.NET

our drivers target the .NET standard 1.3 (see https://github.com/dotnet/standard/blob/master/docs/versions.md).

4.1.4. A "Hello World" example

The example below shows the minimal configuration necessary to interact with Neo4j through a driver.

Example 4.4. Hello World
public class HelloWorldExample : IDisposable
{
    private readonly IDriver _driver;

    public HelloWorldExample(string uri, string user, string password)
    {
        _driver = GraphDatabase.Driver(uri, AuthTokens.Basic(user, password));
    }

    public void PrintGreeting(string message)
    {
        using (var session = _driver.Session())
        {
            var greeting = session.WriteTransaction(tx =>
            {
                var result = tx.Run("CREATE (a:Greeting) " +
                                    "SET a.message = $message " +
                                    "RETURN a.message + ', from node ' + id(a)",
                    new {message});
                return result.Single()[0].As<string>();
            });
            Console.WriteLine(greeting);
        }
    }

    public void Dispose()
    {
        _driver?.Dispose();
    }

    public static void Main()
    {
        using (var greeter = new HelloWorldExample("bolt://localhost:7687", "neo4j", "password"))
        {
            greeter.PrintGreeting("hello, world");
        }
    }
}
public class HelloWorldExample implements AutoCloseable
{
    private final Driver driver;

    public HelloWorldExample( String uri, String user, String password )
    {
        driver = GraphDatabase.driver( uri, AuthTokens.basic( user, password ) );
    }

    @Override
    public void close() throws Exception
    {
        driver.close();
    }

    public void printGreeting( final String message )
    {
        try ( Session session = driver.session() )
        {
            String greeting = session.writeTransaction( new TransactionWork<String>()
            {
                @Override
                public String execute( Transaction tx )
                {
                    StatementResult result = tx.run( "CREATE (a:Greeting) " +
                                                     "SET a.message = $message " +
                                                     "RETURN a.message + ', from node ' + id(a)",
                            parameters( "message", message ) );
                    return result.single().get( 0 ).asString();
                }
            } );
            System.out.println( greeting );
        }
    }

    public static void main( String... args ) throws Exception
    {
        try ( HelloWorldExample greeter = new HelloWorldExample( "bolt://localhost:7687", "neo4j", "password" ) )
        {
            greeter.printGreeting( "hello, world" );
        }
    }
}
const driver = neo4j.driver(uri, neo4j.auth.basic(user, password));
const session = driver.session();

const resultPromise = session.writeTransaction(tx => tx.run(
  'CREATE (a:Greeting) SET a.message = $message RETURN a.message + ", from node " + id(a)',
  {message: 'hello, world'}));

resultPromise.then(result => {
  session.close();

  const singleRecord = result.records[0];
  const greeting = singleRecord.get(0);

  console.log(greeting);

  // on application exit:
  driver.close();
});
class HelloWorldExample(object):

    def __init__(self, uri, user, password):
        self._driver = GraphDatabase.driver(uri, auth=(user, password))

    def close(self):
        self._driver.close()

    def print_greeting(self, message):
        with self._driver.session() as session:
            greeting = session.write_transaction(self._create_and_return_greeting, message)
            print(greeting)

    @staticmethod
    def _create_and_return_greeting(tx, message):
        result = tx.run("CREATE (a:Greeting) "
                        "SET a.message = $message "
                        "RETURN a.message + ', from node ' + id(a)", message=message)
        return result.single()[0]

4.1.5. Driver API docs

For a comprehensive listing of all driver functionality, refer to the API documentation for the specific language driver.