Browse documentation
SDKs

Java SDK

The official Java SDK for the Drishti API provides a thread-safe HTTP client with a method for every endpoint, a managed websocket session that reconnects on its own, and batch job helpers. Its only runtime dependency is Jackson; HTTP and websockets use the JDK's java.net.http.

Requirements

  • Java 17 or later. Check with java -version.
  • A Drishti API key in the DRISHTI_API_KEY environment variable. See the Quickstart to create one.

Installation

<dependency>
  <groupId>io.github.techmanasija</groupId>
  <artifactId>drishti-sdk</artifactId>
  <version>1.0.0</version>
</dependency>

The single JAR bundles Jackson. Use it for scripts and quick tests: java -cp drishti-sdk-1.0.0-all.jar YourProgram.java.

Quick start

Save this as Announcements.java:

java
import com.fasterxml.jackson.databind.JsonNode;
import in.manasija.drishti.DrishtiClient;
import in.manasija.drishti.params.AnnouncementsParams;

public class Announcements {
    public static void main(String[] args) {
        DrishtiClient client = DrishtiClient.fromEnvironment(); // reads DRISHTI_API_KEY

        JsonNode result = client.getAnnouncements(new AnnouncementsParams()
                .symbols("RELIANCE")
                .limit(3)
                .detailed(false));

        for (JsonNode item : result.path("data")) {
            System.out.println(item.path("date").asText() + " | "
                    + item.path("category").asText() + " | "
                    + item.path("summary").asText());
        }
    }
}

Run it with the single JAR:

bash
java -cp drishti-sdk-1.0.0-all.jar Announcements.java

It prints the three latest RELIANCE announcements, one per line, as date | category | summary.

How the client works:

  • JSON responses come back as Jackson JsonNode trees with the field names from the API reference.
  • CSV, JSONL, and HTML responses come back as String, and PDF citations as byte[].
  • Filters are fluent objects from in.manasija.drishti.params. Anything you leave unset is not sent.
  • DrishtiClient is immutable and thread-safe. Create one and share it.

Errors

All SDK exceptions are unchecked and extend DrishtiException:

java
import in.manasija.drishti.exception.DrishtiApiException;
import in.manasija.drishti.exception.DrishtiNetworkException;

try {
    client.getAccount();
} catch (DrishtiApiException e) {      // HTTP error status, after retries
    e.getStatusCode();                  // e.g. 401
    e.getErrorCode();                   // e.g. "unauthorized"
    e.getDetail();                      // e.g. "Invalid API key"
} catch (DrishtiNetworkException e) {   // no response, after retries
    e.getCause();                       // the IOException
}

A blank API key throws IllegalArgumentException before any request is sent.

The client retries 408, 409, 425, 429, 500, 502, 503, and 504 responses, and network errors, up to 2 times with exponential backoff. It follows redirects up to five hops, and never sends your API key to another host, such as the file storage behind PDF citations. See Errors and rate limits for what each status means.

Configuration

java
DrishtiClient client = DrishtiClient.builder()
        .apiKey(System.getenv("DRISHTI_API_KEY"))
        .maxRetries(3)
        .initialRetryDelay(Duration.ofMillis(250))
        .timeout(Duration.ofSeconds(30))
        .build();

To use your own HTTP stack (a proxy, a custom SSL context, or a test double), implement HttpTransport and pass it with .transport(...).

WebSocket session

DrishtiWebSocketSession connects, sends your subscriptions, and dispatches events to handlers. It reconnects with backoff, resubscribes after every reconnect, and treats a silent connection as dropped after 90 seconds. Live streams need a paid plan.

java
DrishtiWebSocketSession ws = client.websocket()
        .onSubscribed(ack -> System.out.println("subscribed " + ack.product() + " on tier " + ack.tier()))
        .onAnnouncements(a -> System.out.println(a.path("symbol").asText() + ": " + a.path("summary").asText()))
        .onError(e -> System.err.println(e.message() + " [" + e.code() + "]"));

ws.subscribe("announcements", List.of("RELIANCE", "TCS"));
ws.run(); // blocks; call ws.stop() from a handler or another thread to end it

In a web app or service, run the session in the background instead, and close it on shutdown:

java
ws.start();  // runs on a daemon thread named drishti-websocket
// ...
ws.close();  // stops the session and waits for the thread

Handlers run on the session thread, one event at a time. Keep them fast and hand heavy work to an executor. An exception thrown in a handler is logged and does not drop the connection.

Batch jobs

Build the JSONL input, submit it, and wait for the results. Each line is one portfolio summary request with a custom_id.

java
String jsonl = Params.buildBatchInputJsonl(List.of(
        Map.of("custom_id", "client-001", "symbols", List.of("TCS"), "mode", "news_context"),
        Map.of("custom_id", "client-002", "symbols", List.of("RELIANCE"), "mode", "news_context")));

JsonNode job = client.submitBatchJobAndWait(
        new BatchJobRequest("watchlist.jsonl", jsonl).displayName("Nightly run"),
        Duration.ofSeconds(5), Duration.ofMinutes(30));

for (ObjectNode result : client.getBatchJobsJobIdResultsParsed(job.path("id").asText())) {
    System.out.println(result.path("custom_id").asText() + " " + result.path("status").asText());
}

See Batch jobs for the input format and limits.

Helper surface

Method names match the JavaScript and PHP SDKs. Filters go in the endpoint's params object, for example getNews(new NewsParams().symbols("TCS").limit(10)).

API wrappers

Workflow helpers

MethodReference
waitForBatchJobCompletionPolls getBatchJobsJobId until a terminal job status is reached.
submitBatchJobAndWaitCalls postBatchJobsFile, then waits for completion.

Transport helpers

MethodReference
requestAny API route: method, path, query, path params, JSON body, and headers. Returns a DrishtiResponse.
requestV1Any /v1/... route; adds the /v1 prefix when it is missing.
get, post, put, patch, deleteShortcuts over request.
websocket, websocketBuilderWebSocket streams

Source and API reference (Javadoc) live in the drishti-sdk repository.