Getting Started — python-binance 0.2.0 documentation (2024)

Installation

python-binance is available on PYPI.Install with pip:

pip install python-binance

Register on Binance

Firstly register an account with Binance.

Generate an API Key

To use signed account methods you are required to create an API Key.

Initialise the client

Pass your API Key and Secret

from binance.client import Clientclient = Client(api_key, api_secret)

or for Asynchronous client

async def main(): # initialise the client client = await AsyncClient.create(api_key, api_secret)if __name__ == "__main__": loop = asyncio.get_event_loop() loop.run_until_complete(main())

Using the Spot, Futures or Vanilla Options Testnet

Binance offers a Spot,Futuresand Vanilla Options Testnet,to test interacting with the exchange.

To enable this set the testnet parameter passed to the Client to True.

The testnet parameter will also be used by any websocket streams when the client is passed to the BinanceSocketManager.

client = Client(api_key, api_secret, testnet=True)

or for Asynchronous client

client = await AsyncClient.create(api_key, api_secret, testnet=True)

Using a different TLD

If you are interacting with a regional version of Binance which has a different TLD such as .us or `.jp’ then youwill need to pass this when creating the client, see examples below.

This tld will also be used by any websocket streams when the client is passed to the BinanceSocketManager.

client = Client(api_key, api_secret, tld='us')

or for Asynchronous client

client = await AsyncClient.create(api_key, api_secret, tld='us')

Making API Calls

Every method supports the passing of arbitrary parameters via keyword matching those in the Binance API documentation.These keyword arguments will be sent directly to the relevant endpoint.

Each API method returns a dictionary of the JSON response as per the Binance API documentation.The docstring of each method in the code references the endpoint it implements.

The Binance API documentation references a timestamp parameter, this is generated for you where required.

Some methods have a recvWindow parameter for timing security, see Binance documentation.

API Endpoints are rate limited by Binance at 20 requests per second, ask them if you require more.

Async API Calls

aiohttp is used to handle asyncio REST requests.

Each function available in the normal client is available in the AsyncClient class.

The only difference is to run within an asyncio event loop and await the function like below.

import asynciofrom binance import AsyncClientasync def main(): client = await AsyncClient.create() # fetch exchange info res = await client.get_exchange_info() print(json.dumps(res, indent=2)) await client.close_connection()if __name__ == "__main__": loop = asyncio.get_event_loop() loop.run_until_complete(main())

Read Async basics for Binancefor more information about asynchronous patterns.

API Rate Limit

Check the get_exchange_info() call for up to date rate limits.

At the current time Binance rate limits are:

  • 1200 weights per minute
  • 10 orders per second
  • 100,000 orders per 24hrs

Some calls have a higher weight than others especially if a call returns information about all symbols.Read the official Binance documentation for specific information.

On each request Binance returns X-MBX-USED-WEIGHT-(intervalNum)(intervalLetter) and X-MBX-ORDER-COUNT-(intervalNum)headers.

Here are examples to access these

Asynchronous example

import asynciofrom binance import AsyncClientapi_key = '<api_key>'api_secret = '<api_secret>'async def main(): client = await AsyncClient.create(api_key, api_secret) res = await client.get_exchange_info() print(client.response.headers) await client.close_connection()if __name__ == "__main__": loop = asyncio.get_event_loop() loop.run_until_complete(main())

Synchronous example

from binance import Clientapi_key = '<api_key>'api_secret = '<api_secret>'def main(): client = Client(api_key, api_secret) res = client.get_exchange_info() print(client.response.headers)if __name__ == "__main__": main()

Requests Settings

python-binance uses the requests library.

You can set custom requests parameters for all API calls when creating the client.

client = Client("api-key", "api-secret", {"verify": False, "timeout": 20})

You may also pass custom requests parameters through any API call to override default settings or the above settingsspecify new ones like the example below.

# this would result in verify: False and timeout: 5 for the get_all_orders callclient = Client("api-key", "api-secret", {"verify": False, "timeout": 20})client.get_all_orders(symbol='BNBBTC', requests_params={'timeout': 5})

Check out the requests documentation for all options.

Proxy Settings

You can use the Requests Settings method above

proxies = { 'http': 'http://10.10.1.10:3128', 'https': 'http://10.10.1.10:1080'}# in the Client instantiationclient = Client("api-key", "api-secret", {'proxies': proxies})# or on an individual callclient.get_all_orders(symbol='BNBBTC', requests_params={'proxies': proxies})

Or set an environment variable for your proxy if required to work across all requests.

An example for Linux environments from the requests Proxies documentation is as follows.

$ export HTTP_PROXY="http://10.10.1.10:3128"$ export HTTPS_PROXY="http://10.10.1.10:1080"

For Windows environments

C:\>set HTTP_PROXY=http://10.10.1.10:3128C:\>set HTTPS_PROXY=http://10.10.1.10:1080

Getting Started — python-binance 0.2.0 documentation (1)

Getting Started — python-binance 0.2.0 documentation (2024)
Top Articles
What is Email Automation? A Beginner's Guide | Mailchimp
10 Biggest Technology Companies
Enrique Espinosa Melendez Obituary
What Happened To Dr Ray On Dr Pol
Gore Videos Uncensored
1movierulzhd.fun Reviews | scam, legit or safe check | Scamadviser
Top Financial Advisors in the U.S.
Melfme
Stream UFC Videos on Watch ESPN - ESPN
A.e.a.o.n.m.s
Chastity Brainwash
Evangeline Downs Racetrack Entries
Alaska: Lockruf der Wildnis
Calmspirits Clapper
Nwi Arrests Lake County
Teenleaks Discord
Find Such That The Following Matrix Is Singular.
Nick Pulos Height, Age, Net Worth, Girlfriend, Stunt Actor
Accuweather Mold Count
1989 Chevy Caprice For Sale Craigslist
Ups Print Store Near Me
The Weather Channel Local Weather Forecast
Talk To Me Showtimes Near Marcus Valley Grand Cinema
Gina Wilson Angle Addition Postulate
Gen 50 Kjv
Aes Salt Lake City Showdown
4.231 Rounded To The Nearest Hundred
Taylored Services Hardeeville Sc
Bj's Tires Near Me
Math Minor Umn
Most popular Indian web series of 2022 (so far) as per IMDb: Rocket Boys, Panchayat, Mai in top 10
Tamilyogi Ponniyin Selvan
Asian Grocery Williamsburg Va
Craigs List Stockton
Überblick zum Barotrauma - Überblick zum Barotrauma - MSD Manual Profi-Ausgabe
Sun Tracker Pontoon Wiring Diagram
Best Restaurants West Bend
Alpha Labs Male Enhancement – Complete Reviews And Guide
Willkommen an der Uni Würzburg | WueStart
3367164101
Race Deepwoken
Online TikTok Voice Generator | Accurate & Realistic
Craigslist Free Cats Near Me
Diamond Desires Nyc
2000 Fortnite Symbols
Frank 26 Forum
Bob Wright Yukon Accident
Saw X (2023) | Film, Trailer, Kritik
Honeybee: Classification, Morphology, Types, and Lifecycle
Supervisor-Managing Your Teams Risk – 3455 questions with correct answers
Latest Posts
Article information

Author: Nicola Considine CPA

Last Updated:

Views: 5636

Rating: 4.9 / 5 (69 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Nicola Considine CPA

Birthday: 1993-02-26

Address: 3809 Clinton Inlet, East Aleisha, UT 46318-2392

Phone: +2681424145499

Job: Government Technician

Hobby: Calligraphy, Lego building, Worldbuilding, Shooting, Bird watching, Shopping, Cooking

Introduction: My name is Nicola Considine CPA, I am a determined, witty, powerful, brainy, open, smiling, proud person who loves writing and wants to share my knowledge and understanding with you.