Neo4j With Scala: Rest End Points with AKKA HTTP

Table of contents
Reading Time: 3 minutes

Hello Folks,

Welcome back again in the series of Neo4j with Scala 😉. Let’s start our journey again. But before starting our journey here is the links for recap who join late us in the journey :

  1. Getting Started Neo4j with Scala : An Introduction
  2. Neo4j with Scala: Defining User Defined Procedures and APOC
  3. Neo4j with Scala: Migrate Data From Other Database to Neo4j
  4. Neo4j with Scala: Awesome Experience with Spark
  5. Neo4j vs ElasticSearch and Full Text Search

Today we will discuss about Neo4j, Scala with Akka-HTTP. We will create rest-endpoints and try to connect with server with them.

The basic idea behind using Akka-HTTP for creating Rest end points is modules implement a full server and client-side HTTP stack on top of akka-actor and akka-stream. We can customize these routes according to our use case. It’s not a web-framework but rather a more general toolkit for providing and consuming HTTP-based services.

We will talk step by step to develop this Rest API after which you will able to run this API on your server and start hitting Rest end points.

Now we start coding :

  1. build.sbt:
    We will start our implementation with the build.sbt file. We will keep all require dependencies in this file.
  2. Factories:
    This is the part where we interact with the Neo4j database. This code shows how to do CRUD operations and Create Relation between the nodes. We will define our Cypher here and execute them. Example:
     def retrieveRecord(email: String): Option[User] = {
        val driver = GraphDatabase.driver(neo4jUrl, AuthTokens.basic(userName, userPassword))
        val session = driver.session
        val script = s"MATCH (a:Users) WHERE = '$email' RETURN AS name, AS " +
                     s"email, a.age AS age, AS city"
        val result =
        val record_data: Option[User] = if (result.hasNext()) {
          val record =
          val results: User = new User(record.get("name").asString(),
        } else {

  3. Routes:
    This is the part where we develop routes to provide rest end points to the client so that they can interact with server and perform the operation which are mention in Factories using REST end points. Example:
    path("get" / "email" / Segment) { (email: String) =>
          get {
            complete {
              try {
                val idAsRDD: Option[User] = retrieveRecord(email)
                idAsRDD match {
                  case Some(data) =>
                    HttpResponse(StatusCodes.OK, entity =
                  case None => HttpResponse(StatusCodes.InternalServerError,
                    entity = s"Data is not fetched and something went wrong")
              } catch {
                case ex: Throwable =>
                  logger.error(ex, ex.getMessage)
                    entity = s"Error found for email : $email")
  4. application.conf:
    This is the part where we define our configuration like http’s interface , port and Neo4j’s URL, userName, userPassword. We define this file in resources folder.
  5. boot.scala:
    This is the file from where we will start the server and create connection with the server.

This is the basic idea for developing a Rest end points with Akka-http. If you want to see code, you can find here or you all can just follow the file and you will ready to hit the server.

This is the basic implementation of how to perform CRUD operation and create relation on Neo4j using the Akka-HTTP.

Here is some example for URL :

This will return the user’s name if user is present in the Neo4j otherwise it will return Data is not fetched and something went wrong.

You can find Activator for the Neo4j with Scala and Akka-http here.


    1. Akka Http Documentation


Written by 

Anurag is the Sr. Software Consultant @ Knoldus Software LLP. In his 3 years of experience, he has become the developer with proven experience in architecting and developing web applications.

1 thought on “Neo4j With Scala: Rest End Points with AKKA HTTP3 min read

Comments are closed.