Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
196 lines (152 sloc) 5.62 KB

This Android SDK is for hauthy and hasuradb APIs.


This SDK is composed of 2 parts. The first part, like a normal SDK, provides an API to login, register, logout and make hasuradb queries. The second part auto-generates relevant classes as a part of your app code so that you can use your hasuradb tables as native Java objects. A gradle task needs to be run to auto-generate this code.

#Usage ##Step 0: Pre-requisites Please make sure that your Gradle is >= 2.12

##Step 1: Include dependencies This SDK is available via jitpack. Add the following code to your build.gradle (at a module level, not necessarily the build.gradle of the entire project).


dependencies {
   compile 'com.github.hasura:baas-sdk-java:c679f89879'

buildscript {
   repositories {
         maven { url "" }
   dependencies {
      classpath 'com.github.hasura.baas-sdk-java:hasura-db-codegen:c679f89879'

task generate << {

   // Read admin credentials
   Properties props = new Properties()
   props.load(new FileInputStream(""))

   // Build Generation Configuration
   // ------------------------------
   def cfg = new io.hasura.db.util.Configuration();

   // Run the code generator
   // ----------------------

##Step 2: Add Hausra admin credentials Next to the build.gradle file where the configuration mentioned above was added, create a new file called The gradle task created above, will use configuration values from this file to automatically create classes in your application module. Let's say that our app module is com.myapp then the following values in will be appropriate.


##Step 3: Add a Hasura singleton class to intialize the DB and Auth components The Hasura singleton class is just a convenient wrapper. You can use it as per your own style. Using the file below as a base is recommended.

Let's say our app module is com.myapp, and our Hasura project name is myproject then we can add a Hasura singleton class as follows:

package com.myapp;

import android.util.Log;

import io.hasura.auth.AuthService;
import io.hasura.db.DBService;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;

public class Hasura {

    public static AuthService auth;
    public static DBService db;
    private Integer userId;
    private String sessionId;
    private String role;

    public static Hasura getInstance() {
        return currentCtx;

    public static void setClient() {

        // Ready
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();

        OkHttpClient c = new OkHttpClient.Builder()
            .addInterceptor(new HasuraTokenInterceptor())

        Hasura.auth = new AuthService("", c);
        Hasura.db = new DBService("", "", c);

    public static Integer getCurrentUserId() {
        return currentCtx.userId;

    public static String getCurrentRole() {
        return currentCtx.role;

    public static String getCurrentSessionId () {
        return currentCtx.sessionId;

    public static void setCurrentSession(Integer userId, String role, String sessionId) {
        Log.d("{{HASURA :: AUTH", "Setting current session");

        if (role == null) {
            role = "anonymous";

        currentCtx.userId = userId;
        currentCtx.sessionId = sessionId;
        currentCtx.role = role;

        // Reset the current client so that we are logged in

    private static final Hasura currentCtx = new Hasura();


Then add one more file as follows to automate the auth token handling:

package com.myapp;

import android.util.Log;


import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class HasuraTokenInterceptor implements Interceptor {
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        Response response;
        Log.d("{{HASURA INTERCEPTOR", request.headers().toString());
        String session = Hasura.getCurrentSessionId();
        String role = Hasura.getCurrentRole();

        if (session == null)  {
            response = chain.proceed(request);
        } else {
            Request newRequest = request.newBuilder()
                    .addHeader("Authorization", "Hasura " + session)
                    .addHeader("X-Hasura-Role", role)
            Log.d("{{HASURA INTERCEPTOR", newRequest.headers().toString());
            response = chain.proceed(newRequest);

        return response;

To install LetsEncrypt certificates

$ wget
$ chmod +x
$ sudo ./ /usr/downloads/android-studio/jre
You can’t perform that action at this time.