Skip to content

tradewatch-io/php-sdk

Repository files navigation

TradeWatch.io PHP SDK

php shield

TradeWatch.io logo

Official SDK for the TradeWatch.io API.

Other SDKs

Python SDK TypeScript SDK .NET SDK Java SDK Go 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.

Requirements

This SDK requires PHP ^8.1.

Installation

composer require tradewatch-io/sdk

Usage

Instantiate and use the client with the following:

<?php

namespace Example;

use Tradewatch\TradewatchClient;
use Tradewatch\Crypto\Requests\CryptoGetQuoteRequest;

$client = new TradewatchClient(
    apiKey: '<value>',
);
$client->crypto->getQuote(
    new CryptoGetQuoteRequest([
        'symbol' => 'BTCUSD',
        'precision' => 2,
    ]),
);

Exception Handling

When the API returns a non-success status code (4xx or 5xx response), an exception will be thrown.

use Tradewatch\Exceptions\TradewatchApiException;
use Tradewatch\Exceptions\TradewatchException;

try {
    $response = $client->crypto->getQuote(...);
} catch (TradewatchApiException $e) {
    echo 'API Exception occurred: ' . $e->getMessage() . "\n";
    echo 'Status Code: ' . $e->getCode() . "\n";
    echo 'Response Body: ' . $e->getBody() . "\n";
    // Optionally, rethrow the exception or handle accordingly.
}

Advanced

Custom Client

This SDK is built to work with any HTTP client that implements the PSR-18 ClientInterface. By default, if no client is provided, the SDK will use php-http/discovery to find an installed HTTP client. However, you can pass your own client that adheres to ClientInterface:

use Tradewatch\TradewatchClient;

// Pass any PSR-18 compatible HTTP client implementation.
// For example, using Guzzle:
$customClient = new \GuzzleHttp\Client([
    'timeout' => 5.0,
]);

$client = new TradewatchClient(options: [
    'client' => $customClient
]);

// Or using Symfony HttpClient:
// $customClient = (new \Symfony\Component\HttpClient\Psr18Client())
//     ->withOptions(['timeout' => 5.0]);
//
// $client = new TradewatchClient(options: [
//     'client' => $customClient
// ]);

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)

Use the maxRetries request option to configure this behavior.

$response = $client->crypto->getQuote(
    ...,
    options: [
        'maxRetries' => 0 // Override maxRetries at the request level
    ]
);

Timeouts

The SDK defaults to a 30 second timeout. Use the timeout option to configure this behavior.

$response = $client->crypto->getQuote(
    ...,
    options: [
        'timeout' => 3.0 // Override timeout at the request level
    ]
);

Contributing

While we value open-source contributions to this SDK, this library is generated programmatically. Additions made directly to this library would have to be moved over to our generation code, otherwise they would be overwritten upon the next generated release. Feel free to open a PR as a proof of concept, but know that we will not be able to merge it as-is. We suggest opening an issue first to discuss with us!

On the other hand, contributions to the README are always very welcome!

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
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 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
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 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
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 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
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 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

Packages

 
 
 

Contributors

Languages