The official PHP client for communicating with the Upstox API.
Upstox API is a set of rest APIs that provide data required to build a complete investment and trading platform. Execute orders in real time, manage user portfolio, stream live market data (using Websocket), and more, with the easy to understand API collection.
This PHP package is automatically generated by the Swagger Codegen project:
PHP 7.4 and later
Run composer require upstox/upstox-php-sdk
to install the SDK from Packagist.
Download the latest release and run composer install
Download the files and include autoload.php
:
require_once('/path/to/UpstoxClient/vendor/autoload.php');
To run the unit tests:
composer install
./vendor/bin/phpunit
Sample Implementations can be found within /examples
folder.
All URIs are relative to https://api.upstox.com/v2/
Class | Method | HTTP request | Description |
---|---|---|---|
ChargeApi | getBrokerage | GET /charges/brokerage | Brokerage details |
HistoryApi | getHistoricalCandleData | GET /historical-candle/{instrumentKey}/{interval}/{to_date} | Historical candle data |
HistoryApi | getHistoricalCandleData1 | GET /historical-candle/{instrumentKey}/{interval}/{to_date}/{from_date} | Historical candle data |
HistoryApi | getIntraDayCandleData | GET /historical-candle/intraday/{instrumentKey}/{interval} | Intra day candle data |
LoginApi | authorize | GET /login/authorization/dialog | Authorize API |
LoginApi | logout | DELETE /logout | Logout |
LoginApi | token | POST /login/authorization/token | Get token API |
MarketQuoteApi | getFullMarketQuote | GET /market-quote/quotes | Market quotes and instruments - Full market quotes |
MarketQuoteApi | getMarketQuoteOHLC | GET /market-quote/ohlc | Market quotes and instruments - OHLC quotes |
MarketQuoteApi | ltp | GET /market-quote/ltp | Market quotes and instruments - LTP quotes. |
OrderApi | cancelOrder | DELETE /order/cancel | Cancel order |
OrderApi | getOrderBook | GET /order/retrieve-all | Get order book |
OrderApi | getOrderDetails | GET /order/history | Get order details |
OrderApi | getTradeHistory | GET /order/trades/get-trades-for-day | Get trades |
OrderApi | getTradesByOrder | GET /order/trades | Get trades for order |
OrderApi | modifyOrder | PUT /order/modify | Modify order |
OrderApi | placeOrder | POST /order/place | Place order |
PortfolioApi | convertPositions | PUT /portfolio/convert-position | Convert Positions |
PortfolioApi | getHoldings | GET /portfolio/long-term-holdings | Get Holdings |
PortfolioApi | getPositions | GET /portfolio/short-term-positions | Get Positions |
TradeProfitAndLossApi | getProfitAndLossCharges | GET /trade/profit-loss/charges | Get profit and loss on trades |
TradeProfitAndLossApi | getTradeWiseProfitAndLossData | GET /trade/profit-loss/data | Get Trade-wise Profit and Loss Report Data |
TradeProfitAndLossApi | getTradeWiseProfitAndLossMetaData | GET /trade/profit-loss/metadata | Get profit and loss meta data on trades |
UserApi | getProfile | GET /user/profile | Get profile |
UserApi | getUserFundMargin | GET /user/get-funds-and-margin | Get User Fund And Margin |
WebsocketApi | getMarketDataFeed | GET /feed/market-data-feed | Market Data Feed |
WebsocketApi | getMarketDataFeedAuthorize | GET /feed/market-data-feed/authorize | Market Data Feed Authorize |
WebsocketApi | getPortfolioStreamFeed | GET /feed/portfolio-stream-feed | Portfolio Stream Feed |
WebsocketApi | getPortfolioStreamFeedAuthorize | GET /feed/portfolio-stream-feed/authorize | Portfolio Stream Feed Authorize |
Connecting to the WebSocket for market and portfolio updates is streamlined through two primary Feeder functions:
Both functions are designed to simplify the process of subscribing to essential data streams, ensuring users have quick and easy access to the information they need.
The MarketDataStreamer
interface is designed for effortless connection to the market WebSocket, enabling users to receive instantaneous updates on various instruments. The following example demonstrates how to quickly set up and start receiving market updates for selected instrument keys:
use Upstox\Client\Configuration;
use Upstox\Client\Feeder\MarketDataStreamer;
use Revolt\EventLoop;
function on_message($streamer, $data)
{
print($data);
}
config = Configuration::getDefaultConfiguration()->setAccessToken(<ACCESS_TOKEN>);
$streamer = new MarketDataStreamer($config, ["NSE_INDEX|Nifty 50", "NSE_INDEX|Bank Nifty"], "full");
$streamer->on("message", 'on_message');
$streamer->connect();
EventLoop::run();
In this example, you first authenticate using an access token, then instantiate MarketDataStreamer with specific instrument keys and a subscription mode. Upon connecting, the streamer listens for market updates, which are logged to the console as they arrive.
Feel free to adjust the access token placeholder and any other specifics to better fit your actual implementation or usage scenario.
full
or ltpc
).The following documentation includes examples to illustrate the usage of these functions and events, providing a practical understanding of how to interact with the MarketDataStreamer effectively.
use Upstox\Client\Configuration;
use Upstox\Client\Feeder\MarketDataStreamer;
use Revolt\EventLoop;
function on_open($streamer)
{
print("Connection Established");
$streamer->subscribe(["NSE_INDEX|Nifty 50", "NSE_INDEX|Bank Nifty"], "full");
}
function on_message($streamer, $data)
{
print($data);
}
config = Configuration::getDefaultConfiguration()->setAccessToken(<ACCESS_TOKEN>);
$streamer = new MarketDataStreamer($config);
$streamer->on("open", 'on_open');
$streamer->on("message", 'on_message');
$streamer->connect();
EventLoop::run();
use Upstox\Client\Configuration;
use Upstox\Client\Feeder\MarketDataStreamer;
use Revolt\EventLoop;
use function Amp\delay;
function on_open($streamer)
{
print("Connection Established");
$streamer->subscribe(["NSE_INDEX|Nifty 50"], "full");
delay(5);
$streamer->subscribe(["NSE_INDEX|Bank Nifty"], "full");
}
function on_message($streamer, $data)
{
print($data);
}
config = Configuration::getDefaultConfiguration()->setAccessToken(<ACCESS_TOKEN>);
$streamer = new MarketDataStreamer($config);
$streamer->on("open", 'on_open');
$streamer->on("message", 'on_message');
$streamer->connect();
EventLoop::run();
use Upstox\Client\Configuration;
use Upstox\Client\Feeder\MarketDataStreamer;
use Revolt\EventLoop;
use function Amp\delay;
function on_open($streamer)
{
print("Connection Established");
$streamer->subscribe(["NSE_EQ|INE020B01018", "NSE_EQ|INE467B01029"]);
delay(5);
$streamer->unsubscribe(["NSE_EQ|INE020B01018", "NSE_EQ|INE467B01029"]);
}
function on_message($streamer, $data)
{
print($data);
}
config = Configuration::getDefaultConfiguration()->setAccessToken(<ACCESS_TOKEN>);
$streamer = new MarketDataStreamer($config);
$streamer->on("open", 'on_open');
$streamer->on("message", 'on_message');
$streamer->connect();
EventLoop::run();
use Upstox\Client\Configuration;
use Upstox\Client\Feeder\MarketDataStreamer;
use Revolt\EventLoop;
use function Amp\delay;
function on_open($streamer)
{
print("Connection Established");
$streamer->subscribe(["NSE_EQ|INE020B01018", "NSE_EQ|INE467B01029"]);
delay(5);
$streamer->changeMode(
["NSE_EQ|INE020B01018", "NSE_EQ|INE467B01029"], "ltpc");
}
function on_message($streamer, $data)
{
print($data);
}
config = Configuration::getDefaultConfiguration()->setAccessToken(<ACCESS_TOKEN>);
$streamer = new MarketDataStreamer($config);
$streamer->on("open", 'on_open');
$streamer->on("message", 'on_message');
$streamer->connect();
EventLoop::run();
use Upstox\Client\Configuration;
use Upstox\Client\Feeder\MarketDataStreamer;
use Revolt\EventLoop;
function on_reconnectstopped($streamer, $data)
{
print($data);
}
config = Configuration::getDefaultConfiguration()->setAccessToken(<ACCESS_TOKEN>);
$streamer = new MarketDataStreamer($config);
$streamer->on("autoReconnectStopped", 'on_reconnectstopped');
$streamer->autoReconnect(false);
$streamer->connect();
EventLoop::run();
use Upstox\Client\Configuration;
use Upstox\Client\Feeder\MarketDataStreamer;
use Revolt\EventLoop;
config = Configuration::getDefaultConfiguration()->setAccessToken(<ACCESS_TOKEN>);
$streamer = new MarketDataStreamer($config);
$streamer->autoReconnect(true, 10, 3);
$streamer->connect();
EventLoop::run();
Connecting to the Portfolio WebSocket for real-time order updates is straightforward with the PortfolioDataStreamer function. Below is a concise guide to get you started on receiving updates:
use Upstox\Client\Configuration;
use Upstox\Client\Feeder\MarketDataStreamer;
use Revolt\EventLoop;
function on_message($streamer, $data)
{
print($data);
}
config = Configuration::getDefaultConfiguration()->setAccessToken(<ACCESS_TOKEN>);
$streamer = new PortfolioDataStreamer($config);
$streamer->on("message", 'on_message');
$streamer->connect();
EventLoop::run();
This example demonstrates initializing the PortfolioDataStreamer, connecting it to the WebSocket, and setting up an event listener to receive and print order updates. Replace