Skip to content

Latest commit

 

History

History
144 lines (99 loc) · 4.59 KB

README.md

File metadata and controls

144 lines (99 loc) · 4.59 KB

Finnhub API  (Kotlin Multiplatform)

Kotlin badge badge badge badge badge badge badge
GitHub release (latest SemVer) Maven Central GitHub

Unofficial kotlin-multiplatform library for Finnhub API. Unlike the official kotlin library, this one supports also WebSockets for real-time updates, uses kotlin coroutines and can be used on multiple platforms (currently android, ios, JVM, macos, windows, linux, web/JS).

Note: Not all endpoints were tested yet, especially the premium ones, please feel free to open an issue or pull request, if any of the endpoints is not working or up-to-date.

Setup

Single platform project / module

Add the finnhub-api dependencies in build.gradle.kts in your module.

val finnhubApiVersion = "0.2.0"

dependencies {
    implementation("com.hibernix.finnhub:finnhub-api-[PLATFORM]:$finnhubApiVersion") // for REST API
    implementation("com.hibernix.finnhub:finnhub-websocket-[PLATFORM]:$finnhubApiVersion") // for real-time updates
}

replace [PLATFORM] with one of platfroms, depending on the platform you are using (e.g. android for Android)

Multiplatform project / module

Add the finnhub-api dependencies in build.gradle.kts in your multiplatform module.

val finnhubApiVersion = "0.2.0"

sourceSets {
    val commonMain by getting {
        dependencies {
            implementation("com.hibernix.finnhub:finnhub-api:$finnhubApiVersion") // for REST API
            implementation("com.hibernix.finnhub:finnhub-websocket:$finnhubApiVersion") // for real-time updates
        }
    }
}

Usage

REST API:

 val finnhubApi = FinnhubApi.create(apiKey = "finnhub_api_key")

scope.launch {
    // company profile
    finnhubApi.companyProfile2("AAPL").let { println(it) }

    // price candles history
    val fromDate = LocalDate.parse("2022-01-01").atStartOfDayIn(TimeZone.UTC)
    finnhubApi.cryptoCandles(
        symbol = "BINANCE:BTCUSDT",
        resolution = "60",
        from = fromDate.epochSeconds,
        to = (fromDate + 3.days).epochSeconds,
    )
        .toCandleList() // convenience method that converts the response to a more useful list of candles
        .forEach { candle -> println("$candle") }
}

WebSocket:

val ws = FinnhubWebSocket.create("finnhub_api_key")

ws.onError = { error -> ... }
ws.connect {
    subscribeTrades("AAPL")
    subscribeNews("GOOGL")
}

ws.tradesFlow.onEach { trades ->  // you can lso use flow.collect {} in coroutine
    trades.forEach { trade -> println("Trade: $trade") }
}.launchIn(GlobalScope) // use your regular scope

ws.newsFlow.onEach { news -> ... }
    .launchIn(GlobalScope) // use your regular scope

Documentation

See official Finnhub API Docs or project docs

Samples

Check some examples in samples folder (coming soon)

ToDo

  • Examples
  • Tests
  • WebSocket ping and auto-reconnect