# Schwab API
**This is not an official API or even a stable recreation of a Charles Schwab API. Functionality may change with any updates made by Schwab. As of April 2023, this API continues to work as expected.**
This package enables buying and selling securities programmatically on Charles Schwab. Currently, we use a headless browser to automate logging in in order to get authorization cookies. Account info and stock positions are pulled through requests. Orders are placed with playwright. This is a regression from the original api from itsjafer, but is necessary for now. During orders the api gets a new authorization key multiple times and I am not sure how to do this with requests.
## Contribution
I would absolutely love contributions; as someone new to open source, I'd appreciate help in setting up a reliable system for PRs as well :)
## Getting Started
### Installing
Install using pypi and then download and install the playwright binaries:
```
pip install schwab-api
python -m playwright install
```
### Create a TOTP Authentication Token
In order to login to Schwab without having to go through SMS verification everytime, we can create an authentication token (TOTP) and attach that to our account.
Alternatively, you can do this programmatically:
```
from schwab_api2 import generate_totp
symantec_id, totp_secret = generate_totp()
print("Your symantec ID is: " + symantec_id)
print("Your TOTP secret is: " + totp_secret)
```
For the TOTP Secret:
1. Download Duo Mobile, Google Authenticator, or any other authenticator of your choice and create an entry using the TOTP secret. You will be prompted to generate a code everytime you log in to Schwab and will need an authenticator app to do so.
2. **Keep this TOTP secret handy** as you'll need to pass it to this API in order to login.
For the Symantec ID:
1. Log in to Schwab and go to your [security center](https://client.schwab.com/clientapps/access/securityCenter#/main/epass).
1. Under two-step verification, select "Always at login", and then select "Security Token".
1. **Enter the symantec ID here that you generated using the code above**.
### Quickstart
Here's some code that logs in, gets all account holdings, and makes a stock purchase:
```
from schwab_api2 import Schwab
import pprint
# Change these variables
username = "username"
password = "password"
totp_secret = "ABCDEF1234567890"
# Create the schwab instance
api = Schwab(
headless=True,
browserType="firefox"
)
# Login using playwright
print("Logging into Schwab")
logged_in = api.login(
username=username,
password=password,
totp_secret=totp_secret
)
# Get information about all accounts holdings
print("Getting account holdings information")
account_info = api.get_account_info()
pprint.pprint(account_info)
accounts = list(account_info.keys())
print(f"The following account numbers were found: {accounts}")
# Place a dry run trade for each account
print("Placing a dry run trade for INTC stock")
for account in accounts:
messages, success = api.trade(
ticker="INTC",
action="Buy",
qty=1,
order_type="Market",
account_id=str(account),
price=0.00,
dry_run=True
)
print("The order verification was " + "successful" if success else "unsuccessful")
print("The order verification produced the following messages: ")
pprint.pprint(messages)
api.close_session()
```
## Features
* Buying and Selling tickers
* Multiple individual account support
* MFA and TOTP authentication
* Account and Position Information
* Headless playwright implementation
## TODO
* Add Cancel Orders
* Add Options
* Give me Ideas!
Raw data
{
"_id": null,
"home_page": "https://github.com/MaxxRK/schwab-api2",
"name": "schwab-api2",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "schwab,python3,api,unofficial,schwab-api,schwab charles api",
"author": "MaxxRK",
"author_email": "maxxrk@pm.me",
"download_url": "https://files.pythonhosted.org/packages/53/81/e4d7be77c1dc49e5c4e0f059e129abe8f1abe89bce73f782369493d5fe94/schwab_api2-0.2.9.tar.gz",
"platform": null,
"description": "# Schwab API\r\n\r\n**This is not an official API or even a stable recreation of a Charles Schwab API. Functionality may change with any updates made by Schwab. As of April 2023, this API continues to work as expected.**\r\n\r\nThis package enables buying and selling securities programmatically on Charles Schwab. Currently, we use a headless browser to automate logging in in order to get authorization cookies. Account info and stock positions are pulled through requests. Orders are placed with playwright. This is a regression from the original api from itsjafer, but is necessary for now. During orders the api gets a new authorization key multiple times and I am not sure how to do this with requests.\r\n\r\n\r\n## Contribution\r\n\r\nI would absolutely love contributions; as someone new to open source, I'd appreciate help in setting up a reliable system for PRs as well :)\r\n\r\n## Getting Started\r\n\r\n### Installing\r\n\r\nInstall using pypi and then download and install the playwright binaries:\r\n\r\n```\r\npip install schwab-api\r\npython -m playwright install\r\n```\r\n\r\n### Create a TOTP Authentication Token\r\n\r\nIn order to login to Schwab without having to go through SMS verification everytime, we can create an authentication token (TOTP) and attach that to our account.\r\n\r\nAlternatively, you can do this programmatically:\r\n\r\n```\r\nfrom schwab_api2 import generate_totp\r\n\r\nsymantec_id, totp_secret = generate_totp()\r\n\r\nprint(\"Your symantec ID is: \" + symantec_id)\r\nprint(\"Your TOTP secret is: \" + totp_secret)\r\n```\r\n\r\nFor the TOTP Secret:\r\n\r\n1. Download Duo Mobile, Google Authenticator, or any other authenticator of your choice and create an entry using the TOTP secret. You will be prompted to generate a code everytime you log in to Schwab and will need an authenticator app to do so.\r\n2. **Keep this TOTP secret handy** as you'll need to pass it to this API in order to login.\r\n\r\nFor the Symantec ID:\r\n\r\n1. Log in to Schwab and go to your [security center](https://client.schwab.com/clientapps/access/securityCenter#/main/epass). \r\n1. Under two-step verification, select \"Always at login\", and then select \"Security Token\". \r\n1. **Enter the symantec ID here that you generated using the code above**.\r\n\r\n### Quickstart\r\n\r\nHere's some code that logs in, gets all account holdings, and makes a stock purchase:\r\n```\r\nfrom schwab_api2 import Schwab\r\nimport pprint\r\n\r\n# Change these variables\r\nusername = \"username\"\r\npassword = \"password\"\r\ntotp_secret = \"ABCDEF1234567890\"\r\n\r\n# Create the schwab instance\r\napi = Schwab(\r\n headless=True,\r\n browserType=\"firefox\"\r\n)\r\n\r\n# Login using playwright\r\nprint(\"Logging into Schwab\")\r\nlogged_in = api.login(\r\n username=username,\r\n password=password,\r\n totp_secret=totp_secret\r\n)\r\n\r\n# Get information about all accounts holdings\r\nprint(\"Getting account holdings information\")\r\naccount_info = api.get_account_info()\r\npprint.pprint(account_info)\r\naccounts = list(account_info.keys())\r\nprint(f\"The following account numbers were found: {accounts}\")\r\n\r\n\r\n# Place a dry run trade for each account\r\nprint(\"Placing a dry run trade for INTC stock\")\r\n\r\nfor account in accounts:\r\n messages, success = api.trade(\r\n ticker=\"INTC\",\r\n action=\"Buy\",\r\n qty=1,\r\n order_type=\"Market\",\r\n account_id=str(account),\r\n price=0.00,\r\n dry_run=True\r\n )\r\n\r\n print(\"The order verification was \" + \"successful\" if success else \"unsuccessful\")\r\n print(\"The order verification produced the following messages: \")\r\n pprint.pprint(messages)\r\n\r\napi.close_session()\r\n```\r\n\r\n## Features\r\n\r\n* Buying and Selling tickers\r\n* Multiple individual account support\r\n* MFA and TOTP authentication\r\n* Account and Position Information\r\n* Headless playwright implementation\r\n\r\n## TODO\r\n* Add Cancel Orders\r\n* Add Options\r\n* Give me Ideas!\r\n\r\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Unofficial Schwab API wrapper in Python 3.",
"version": "0.2.9",
"project_urls": {
"Download": "https://github.com/MaxxRK/schwab-api2/releases/tag/V_029",
"Homepage": "https://github.com/MaxxRK/schwab-api2"
},
"split_keywords": [
"schwab",
"python3",
"api",
"unofficial",
"schwab-api",
"schwab charles api"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "5381e4d7be77c1dc49e5c4e0f059e129abe8f1abe89bce73f782369493d5fe94",
"md5": "085aeb2687b1b3095e4fa3ff2fbf175e",
"sha256": "3c3d57d14c25668b069ebe244d9c76790a1f5d5d1b7b7d02e6d93fb6af62ebd4"
},
"downloads": -1,
"filename": "schwab_api2-0.2.9.tar.gz",
"has_sig": false,
"md5_digest": "085aeb2687b1b3095e4fa3ff2fbf175e",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 11197,
"upload_time": "2023-10-25T02:41:24",
"upload_time_iso_8601": "2023-10-25T02:41:24.415669Z",
"url": "https://files.pythonhosted.org/packages/53/81/e4d7be77c1dc49e5c4e0f059e129abe8f1abe89bce73f782369493d5fe94/schwab_api2-0.2.9.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-10-25 02:41:24",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "MaxxRK",
"github_project": "schwab-api2",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "schwab-api2"
}