Skip to content

pseudomarkets/FinnHubSharp

Repository files navigation

FinnHubSharp

FinnHubSharp is a .NET Standard client for the Finnhub finance APIs. It supports common REST endpoints and streaming trade data over WebSockets.

Requirements

  • .NET SDK 10.0 or later for building this repository
  • Library target: netstandard2.1
  • Test target: net10.0

Repository Layout

src/
  FinnHubSharp/              # Client library
tests/
  FinnHubSharp.Tests/        # NUnit unit tests
utilities/
  FinnHubSharpConsole/       # Console runner/sample app

The solution includes src, tests, and utilities as solution folders for IDEs such as Visual Studio and Rider.

Install

NuGet package: FinnHubSharp

dotnet add package FinnHubSharp

Usage

REST Client

using FinnHubSharp.Implementations;
using FinnHubSharp.Models.Configuration;

var httpClient = new HttpClient();
var client = new FinnHubClient(
    httpClient,
    new FinnHubSharpConfiguration
    {
        ApiKey = "YOUR_API_KEY"
    });

var quote = await client.GetQuoteAsync("AAPL");

if (quote.ErrorMessage is null)
{
    Console.WriteLine($"Current price for AAPL: {quote.Quote.CurrentPrice}");
}
else
{
    Console.WriteLine($"Finnhub request failed: {quote.ErrorMessage}");
}

Available REST methods:

  • GetQuoteAsync(string symbol)
  • GetSymbolInfoAsync(string symbolOrSecurityName)
  • GetAllSymbolsAsync(string exchange)
  • GetMarketStatusAsync(string exchange)
  • GetMarketHolidaysAsync(string exchange)

Streaming Client

using System.Net.WebSockets;
using FinnHubSharp.Implementations;
using FinnHubSharp.Models.Request;

var streamerClient = new FinnHubStreamerClient(new ClientWebSocket(), "YOUR_API_KEY");

await foreach (var stream in streamerClient.GetStreamingQuotes(
    new StreamingSubscription
    {
        Type = "subscribe",
        Symbol = "BINANCE:BTCUSDT"
    }))
{
    await foreach (var message in stream)
    {
        foreach (var trade in message.Data)
        {
            Console.WriteLine($"{trade.Symbol}: {trade.Price}");
        }
    }
}

JSON

FinnHubSharp uses System.Text.Json for JSON serialization and deserialization. The raw response models use JsonPropertyName attributes to preserve Finnhub wire names such as c, pc, displaySymbol, and type.

Development

Restore and build:

dotnet restore FinnHubSharp.sln
dotnet build FinnHubSharp.sln --no-restore

Run tests:

dotnet test tests/FinnHubSharp.Tests/FinnHubSharp.Tests.csproj --no-restore

The test suite uses NUnit, Moq, RichardSzalay.MockHttp, and Shouldly.

Run the console sample:

dotnet run --project utilities/FinnHubSharpConsole/FinnHubSharpConsole.csproj

License

MIT

About

.NET Standard 2.1 client for accessing FinnHub.io finance APIs, including streaming data via WebSocket

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages