Skip to content

tradewatch-io/go-sdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TradeWatch.io Go SDK

TradeWatch.io logo

Official SDK for the TradeWatch.io API.

Other SDKs

Python SDK TypeScript SDK .NET SDK PHP SDK Java SDK Ruby SDK Swift SDK Rust SDK

What is TradeWatch.io?

TradeWatch.io is a market data platform and API for real-time and historical prices across crypto, stocks, indices, currencies, and commodities.

Try the Interactive API Playground

Want to test endpoints without writing code first? Use the TradeWatch Interactive API Playground to run requests directly in your browser.

TradeWatch Interactive API Playground

Resources

Quick Start

  1. Create an API key in the TradeWatch Dashboard.
  2. Follow platform setup docs: Getting started.

Table of Contents

Documentation

API reference documentation is available here.

Reference

A full reference for this library is available here.

Usage

Instantiate and use the client with the following:

package example

import (
    client "sdk/client"
    option "sdk/option"
    sdk "sdk"
    context "context"
)

func do() {
    client := client.NewClient(
        option.WithAPIKey(
            "<value>",
        ),
    )
    request := &sdk.CryptoGetQuoteRequest{
        Symbol: "BTC-USD",
        Precision: sdk.Int(
            2,
        ),
    }
    client.Crypto.GetQuote(
        context.TODO(),
        request,
    )
}

Environments

You can choose between different environments by using the option.WithBaseURL option. You can configure any arbitrary base URL, which is particularly useful in test environments.

client := client.NewClient(
    option.WithBaseURL(api.Environments.Default),
)

Errors

Structured error types are returned from API calls that return non-success status codes. These errors are compatible with the errors.Is and errors.As APIs, so you can access the error like so:

response, err := client.Crypto.GetQuote(...)
if err != nil {
    var apiError *core.APIError
    if errors.As(err, apiError) {
        // Do something with the API error ...
    }
    return err
}

Request Options

A variety of request options are included to adapt the behavior of the library, which includes configuring authorization tokens, or providing your own instrumented *http.Client.

These request options can either be specified on the client so that they're applied on every request, or for an individual request, like so:

Providing your own *http.Client is recommended. Otherwise, the http.DefaultClient will be used, and your client will wait indefinitely for a response (unless the per-request, context-based timeout is used).

// Specify default options applied on every request.
client := client.NewClient(
    option.WithToken("<YOUR_API_KEY>"),
    option.WithHTTPClient(
        &http.Client{
            Timeout: 5 * time.Second,
        },
    ),
)

// Specify options for an individual request.
response, err := client.Crypto.GetQuote(
    ...,
    option.WithToken("<YOUR_API_KEY>"),
)

Advanced

Response Headers

You can access the raw HTTP response data by using the WithRawResponse field on the client. This is useful when you need to examine the response headers received from the API call. (When the endpoint is paginated, the raw HTTP response data will be included automatically in the Page response object.)

response, err := client.Crypto.WithRawResponse.GetQuote(...)
if err != nil {
    return err
}
fmt.Printf("Got response headers: %v", response.Header)
fmt.Printf("Got status code: %d", response.StatusCode)

Retries

The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long as the request is deemed retryable and the number of retry attempts has not grown larger than the configured retry limit (default: 2).

A request is deemed retryable when any of the following HTTP status codes is returned:

  • 408 (Timeout)
  • 429 (Too Many Requests)
  • 5XX (Internal Server Errors)

If the Retry-After header is present in the response, the SDK will prioritize respecting its value exactly over the default exponential backoff.

Use the option.WithMaxAttempts option to configure this behavior for the entire client or an individual request:

client := client.NewClient(
    option.WithMaxAttempts(1),
)

response, err := client.Crypto.GetQuote(
    ...,
    option.WithMaxAttempts(1),
)

Timeouts

Setting a timeout for each individual request is as simple as using the standard context library. Setting a one second timeout for an individual API call looks like the following:

ctx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()

response, err := client.Crypto.GetQuote(ctx, ...)

Explicit Null

If you want to send the explicit null JSON value through an optional parameter, you can use the setters
that come with every object. Calling a setter method for a property will flip a bit in the explicitFields bitfield for that setter's object; during serialization, any property with a flipped bit will have its omittable status stripped, so zero or nil values will be sent explicitly rather than omitted altogether:

type ExampleRequest struct {
    // An optional string parameter.
    Name *string `json:"name,omitempty" url:"-"`

    // Private bitmask of fields set to an explicit value and therefore not to be omitted
    explicitFields *big.Int `json:"-" url:"-"`
}

request := &ExampleRequest{}
request.SetName(nil)

response, err := client.Crypto.GetQuote(ctx, request, ...)

Available Methods

Account

Method Required Params Summary Description
GetUsage() - Usage statistics Get the usage statistics of your API account

Currencies

Method Required Params Summary Description
Convert(from, to) from, to Conversion Convert one symbol to another
GetInsights() - Get Insights Get recent currencies insights.
GetQuote(symbol) symbol Last Quote Get the last quote tick for the provided symbol.
GetQuotes(symbols) symbols Last Quotes Get the last quote tick for the provided symbols.
GetSymbols() - Available Symbols Get list of available symbols

Crypto

Method Required Params Summary Description
Convert(from, to) from, to Conversion Convert one symbol to another
GetExchanges() - Available Exchanges Get list of available cryptocurrency exchanges
GetInsights() - Get Insights Get recent crypto insights.
GetQuote(symbol) symbol Last Quote Get the last quote tick for the provided symbol.
GetQuotes(symbols) symbols Last Quotes Get the last quote tick for the provided symbols.
GetSymbols() - Available Symbols Get list of available symbols

Indices

Method Required Params Summary Description
GetInsights() - Get Insights Get recent indices insights.
GetQuote(symbol) symbol Last Quote Get the last quote tick for the provided symbol.
GetQuotes(symbols) symbols Last Quotes Get the last quote tick for the provided symbols.
GetSymbols() - Available Symbols Get list of available symbols

Stocks

Method Required Params Summary Description
GetHistoricalOhlc(symbol, resolution, start, end) symbol, resolution, start, end Get Historical Ohlc Get historical OHLC candles for a symbol in a selected resolution and time range.
GetHistoricalTicks(symbol, start, end) symbol, start, end Get Historical Ticks Get raw historical ticks for a symbol in a selected time range using cursor pagination.
GetInsights() - Get Insights Get recent stocks insights.
GetMarketHolidays(start, end) start, end Get Market Holidays Get market holidays. It takes half-days into account.
GetMarketStatus() - Get Market Status Get the current status (open or closed) of a market. It takes holidays and half-days into account but does not factor in circuit breakers or halts.
GetMarkets() - Get Markets Get details about the markets available in this API.
GetQuote(symbol) symbol Last Quote Get the last quote tick for the provided symbol.
GetQuotes(symbols) symbols Last Quotes Get the last quote tick for the provided symbols.
GetStockData(symbol) symbol Get Stock Data Get Stock Data
GetSymbols() - Available Symbols Get list of available symbols
GetTradingHours(start, end) start, end Get Trading Hours Get trading hours. It takes half-days into account.
StockGetCountries() - Available Countries Get list of available countries

Commodities

Method Required Params Summary Description
GetInsights() - Get Insights Get recent commodities insights.
GetQuote(symbol) symbol Last Quote Get the last quote tick for the provided symbol.
GetQuotes(symbols) symbols Last Quotes Get the last quote tick for the provided symbols.
GetSymbols() - Available Symbols Get list of available symbols
GetTypes() - Available Types Get list of available commodity types

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages