{"id":15296333,"url":"https://github.com/jeffreyhyer/alpaca-trade-api-php","last_synced_at":"2026-03-06T16:34:53.715Z","repository":{"id":49822347,"uuid":"174259786","full_name":"JeffreyHyer/alpaca-trade-api-php","owner":"JeffreyHyer","description":"PHP SDK for the Alpaca trade API","archived":false,"fork":false,"pushed_at":"2023-05-22T17:09:58.000Z","size":22,"stargazers_count":27,"open_issues_count":1,"forks_count":16,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-27T10:21:17.920Z","etag":null,"topics":["alpaca","api","php","php-library","php7","sdk","sdk-php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JeffreyHyer.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-03-07T02:51:48.000Z","updated_at":"2024-08-22T10:56:34.000Z","dependencies_parsed_at":"2024-11-07T23:01:29.199Z","dependency_job_id":null,"html_url":"https://github.com/JeffreyHyer/alpaca-trade-api-php","commit_stats":{"total_commits":19,"total_committers":4,"mean_commits":4.75,"dds":0.1578947368421053,"last_synced_commit":"aa33fe7442e6ef467a1c6688b107a01e4cc43751"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JeffreyHyer%2Falpaca-trade-api-php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JeffreyHyer%2Falpaca-trade-api-php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JeffreyHyer%2Falpaca-trade-api-php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JeffreyHyer%2Falpaca-trade-api-php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JeffreyHyer","download_url":"https://codeload.github.com/JeffreyHyer/alpaca-trade-api-php/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248767891,"owners_count":21158554,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["alpaca","api","php","php-library","php7","sdk","sdk-php"],"created_at":"2024-09-30T18:10:08.597Z","updated_at":"2026-03-06T16:34:48.685Z","avatar_url":"https://github.com/JeffreyHyer.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Alpaca PHP SDK\n\n![Packagist Version](https://img.shields.io/packagist/v/jeffreyhyer/alpaca-trade-api-php?label=Packagist)\n![Packagist](https://img.shields.io/packagist/dt/jeffreyhyer/alpaca-trade-api-php?color=blue\u0026label=Downloads)\n\nThis repository contains a PHP SDK for use with the\n[Alpaca](https://alpaca.markets?ref_by=858915e73e) API.\n\n**DISCLAIMER:** This is **NOT** an official SDK, it is not affiliated\nwith nor endorsed by Alpaca in any way.\n\n## Release Notes\n\n#### v3.0.0\n\nWith the release of `v3.0.0` of this library we use the Alpaca Data API v2.\nThe `getLastTrade` and `getLastQuote` methods are backwards compatible and\nrequire no changes to your codebase.\n\n:warning: The `getBars` method is NOT backwards compatible so you will need to update\nyour codebase to use the new method signature found in the Market Data section below.\n\n#### v2.0.0\n\nWith the release of `v2.0.0` of this library we use v2 of the Alpaca API.\nHowever, the methods are all backwards compatible with v1.0.0 of this\nlibrary and v1 of the Alpaca API so upgrading should be as simple as\nupdating the package version in your `composer.json` file and installing.\nEverything should work as it did before but now you'll have access to\nthe new methods and new method parameters.\n\n## Installation\n\n**NOTE**: This package currently requires PHP \u003e= 7.2.5\n\n```shell\n$ composer require jeffreyhyer/alpaca-trade-api-php\n```\n\n## Usage\n\nFrom within your PHP application you can access the Alpaca API with\njust a couple of lines:\n\n```php\n\u003c?php\n\nrequire './vendor/autoload.php';\n\nuse Alpaca\\Alpaca;\n\n$alpaca = new Alpaca(\"--KEY--\", \"--SECRET--\");\n\n$positions = $alpaca-\u003egetPositions();\n```\n\n## OAuth\n\nFrom `v2.1.0` this package supports authenticating users via OAuth to\nthe Alpaca API. For a detailed explanation on the OAuth flow, see\n[the Alpaca Docs](https://docs.alpaca.markets/build-apps_services-with-alpaca/oauth-guide/#integration).\n\nThe following methods can be used to request Authorization to access\nan external users Alpaca account and retrieve an access token to make\nAPI calls on their behalf.\n\n**`getOauthAuthorizeUrl($client_id, $redirect_uri, $scope = \"\", $state = null)`**\n\nProvide your applications `$client_id` and an authorized/whitelisted\n`$redirect_uri` as well as your desired `$scope` and a random `$state`\nvalue. This function will return the URL to which you should redirect\nyour user to in order to Authorize your application to access their account.\n\n**`getOauthAccessToken($code, $client_id, $client_secret, $redirect_uri)`**\n\nOnce the user has authorized your application to access their account, Alpaca\nwill redirect the user back to your application (`$redirect_uri`). In the URL\nwill be a `code` parameter, you will pass that as the `$code` parameter to this\nfunction along with your `$client_id` and `$client_secret` and your original\n`$redirect_url`. This function will return an access token that can then be used\nto call the Alpaca API on behalf of that user/account.\n\nTo start using the access token call **`setAccessToken($token)`** with the value of the token.\n\nOnce authenticated, you can call **`getOauthAccessTokenDetails()`** to\nget the details of the access token (status, validity, etc).\n\n## API\n\n### Constructor\n\nTo get started initialize the `Alpaca` class with your Alpaca Key and\nSecret. You can also set the third parameter to enable or disable\npaper trading. The default is `true` to enable calling against the\npaper trading endpoint.\n\n```php\nuse Alpaca\\Alpaca;\n\n$alpaca = new Alpaca(\"KEY\", \"SECRET\", true);\n\n// This call will now work as expected if your KEY and SECRET are valid.\n// If not, the response will contain an error explaining what went wrong.\n$resp = $alpaca-\u003egetAccount();\n```\n\nYou can change these values after initialization if necessary using\nthe following methods:\n\n**`setKey($key)`**\n\nSet your Alpaca Key\n\n**`setSecret($secret)`**\n\nSet your Alpaca Secret\n\n**`setPaper(true)`**\n\nEnable or disable paper trading. `true` = Paper Trading, `false` =\nLive Trading.\n\n---\n\n### Response\n\nAll methods return an instance of the `\\Alpaca\\Response` class which\nhas a number of convenient methods for working with the API response.\n\n**`getCode()`**\n\nReturns the HTTP status code of the request (e.g. `200`, `403`, etc).\n\n**`getReason()`**\n\nReturns the HTTP status reason of the request (e.g. `OK`, `Forbidden`,\netc).\n\n**`getResponse()`**\n\nReturns the JSON decoded response. For example:\n\n```php\nprint_r($alpaca-\u003egetAccount()-\u003egetResponse());\n\n/*\nResults in:\n\nstdClass Object\n(\n    [id] =\u003e null\n    [status] =\u003e ACTIVE\n    [currency] =\u003e USD\n    [buying_power] =\u003e 25000\n    [cash] =\u003e 25000\n    [cash_withdrawable] =\u003e 0\n    [portfolio_value] =\u003e 25000\n    [pattern_day_trader] =\u003e \n    [trading_blocked] =\u003e \n    [transfers_blocked] =\u003e \n    [account_blocked] =\u003e \n    [created_at] =\u003e 2018-11-01T18:41:35.990779Z\n    [trade_suspended_by_user] =\u003e \n)\n*/\n```\n\n---\n\n### Account\n\n:ledger: [Alpaca Docs](https://docs.alpaca.markets/api-documentation/api-v2/account/)\n\n**`getAccount()`**\n\nReturns the account associated with the API key.\n\n---\n\n### Orders\n\n:ledger: [Alpaca Docs](https://docs.alpaca.markets/api-documentation/api-v2/orders/)\n\n**`getOrders($status = null, $limit = null, $after = null, $until = null, $direction = null, $nested = null)`**\n\nRetrieves a list of orders for the account, optionally filtered by the\nsupplied query parameters.\n\n**`createOrder($symbol, $qty, $side, $type, $time_in_force, $limit_price = null, $stop_price = null, $client_order_id = null, $extended_hours = null, $order_class = null, $additional = [])`**\n\nPlaces a new order for the given account. An order request may be\nrejected if the account is not authorized for trading, or if the\ntradable balance is insufficient to fill the order.\n\n**`getOrder($order_id)`**\n\nRetrieves a single order for the given `$order_id`.\n\n**`getOrderByClientId($client_order_id)`**\n\nRetrieves a single order for the given `$client_order_id`.\n\n**`replaceOrder($order_id, $qty, $time_in_force, $limit_price = null, $stop_price = null, $client_order_id = null)`**\n\nReplaces a single order with updated parameters. Each parameter\noverrides the corresponding attribute of the existing order. The other\nattributes remain the same as the existing order.\n\n**`cancelOrder($order_id)`**\n\nAttempts to cancel an open order. If the order is no longer cancelable\n(example: `status=order_filled`), the server will respond with status\n422, and reject the request.\n\n**`cancelAllOrders()`**\n\nAttempts to cancel all open orders. A response will be provided for\neach order that is attempted to be cancelled. If an order is no\nlonger cancelable, the server will respond with status 500 and reject\nthe request.\n\n---\n\n### Positions\n\n:ledger: [Alpaca Docs](https://docs.alpaca.markets/api-documentation/api-v2/positions/)\n\n**`getPositions()`**\n\nRetrieves a list of the account's open positions.\n\n**`getPosition($symbol)`**\n\nRetrieves the account's open position for the given `$symbol`.\n\n**`closeAllPositions()`**\n\nCloses (liquidates) all of the account’s open long and short positions.\nA response will be provided for each order that is attempted to be\ncancelled. If an order is no longer cancelable, the server will respond\nwith status 500 and reject the request.\n\n**`closePosition($symbol)`**\n\nCloses (liquidates) the account’s open position for the given `$symbol`.\nWorks for both long and short positions.\n\n---\n\n### Assets\n\n:ledger: [Alpaca Docs](https://docs.alpaca.markets/api-documentation/api-v2/assets/)\n\n**`getAssets($status = null, $asset_class = null)`**\n\nGet a list of assets\n\n**`getAsset($symbol)`**\n\nGet an asset for the given `$symbol`.\n\n**`getAssetById($id)`**\n\nGet an asset for the given `$id`.\n\n---\n\n### Watchlists\n\n:ledger: [Alpaca Docs](https://docs.alpaca.markets/api-documentation/api-v2/watchlist/)\n\n**`getWatchlists()`**\n\nReturns the list of watchlists registered under the account.\n\n**`createWatchlist($name, $symbols = [])`**\n\nCreate a new watchlist with initial set of assets (`$symbols`). The\n`$name` is used to identify the watchlist in any of the `*WatchlistByName()`\nmethods below.\n\n**`getWatchlist($id)`**\n\nReturns a watchlist identified by the `$id`.\n\n**`getWatchlistByName($name)`**\n\nReturns a watchlist identified by the `$name`.\n\n**`updateWatchlist($id, $name, $symbols = [])`**\n\nUpdate the name and/or content of the watchlist identified by `$id`.\n\n**`updateWatchlistByName($name, $symbols = [])`**\n\nUpdate the name and/or content of the watchlist identified by `$name`.\n\n**`addAssetToWatchlist($id, $symbol)`**\n\nAppend an asset for the `$symbol` to the end of watchlist asset list.\n\n**`addAssetToWatchlistByName($name, $symbol)`**\n\nAppend an asset for the `$symbol` to the end of watchlist asset list.\n\n**`deleteWatchlist($id)`**\n\nDelete a watchlist. This is a permanent deletion.\n\n**`deleteWatchlistByName($name)`**\n\nDelete a watchlist. This is a permanent deletion.\n\n---\n\n### Calendar\n\n:ledger: [Alpaca Docs](https://docs.alpaca.markets/api-documentation/api-v2/calendar/)\n\n**`getCalendar($start = null, $end = null)`**\n\nReturns the market calendar.\n\n---\n\n### Clock\n\n:ledger: [Alpaca Docs](https://docs.alpaca.markets/api-documentation/api-v2/clock/)\n\n**`getClock()`**\n\nReturns the market clock.\n\n---\n\n### Account Configurations\n\n:ledger: [Alpaca Docs](https://docs.alpaca.markets/api-documentation/api-v2/account-configuration/)\n\n**`getAccountConfigurations()`**\n\nReturns the current account configuration values.\n\n**`updateAccountConfigurations($config = [])`**\n\nUpdates and returns the current account configuration values.\n`$config` is an array of key-value pairs (e.g. `[\"key\" =\u003e \"value\"]`.\n\n---\n\n### Account Activities\n\n:ledger: [Alpaca Docs](https://docs.alpaca.markets/api-documentation/api-v2/account-activities/)\n\n**`getAccountActivitiesOfType($type, $date = null, $until = null, $after = null, $direction = null, $page_size = null, $page_token = null)`**\n\nReturns account activity entries for a specific type of activity.\n\n**`getAccountActivities($types = [])`**\n\nReturns account activity entries for many types of activities.\n\n---\n\n### Portfolio History\n\n:ledger: [Alpaca Docs](https://docs.alpaca.markets/api-documentation/api-v2/portfolio-history/)\n\n**`getPortfolioHistory($period = null, $timeframe = null, $date_end = null, $extended_hours = null)`**\n\nReturns timeseries data about equity and profit/loss (P/L) of the\naccount in requested timespan.\n\n---\n\n### Market Data (v2 API)\n\n:ledger: [Alpaca Docs](https://alpaca.markets/docs/api-documentation/api-v2/market-data/alpaca-data-api-v2/historical/)\n\n**`getTrades($symbol, $start, $end, $limit = null, $page_token = null)`**\n\nReturns trade historical data for the requested security.\n\n**`getLastTrade($symbol)`**\n\nRetrieve the last trade for the requested symbol.\n\n**`getQuotes($symbol, $start, $end, $limit = null, $page_token = null)`**\n\nReturns quote (NBBO) historical data for the requested security.\n\n**`getLastQuote($symbol)`**\n\nRetrieves the last quote for the requested symbol.\n\n**`getBars($timeframe, $symbol, $start, $end, $limit = null, $page_token = null)`**\n\nReturns aggregate historical data for the requested security.\n\n**`getMultiSnapshot($symbols)`**\n\nReturns the snapshots for the requested securities.\n\n**`getSnapshot($symbol)`**\n\nReturns the snapshot for the requested security.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjeffreyhyer%2Falpaca-trade-api-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjeffreyhyer%2Falpaca-trade-api-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjeffreyhyer%2Falpaca-trade-api-php/lists"}