steampy Documentation

repository·master·Indexed 20 days ago

https://github.com/bukson/steampy

A lightweight Python library for interacting with Steam, specifically designed for managing trade offers, market listings, and inventories. steampy supports SteamGuard authentication, proxy usage, and session cookie injection. Key features include the SteamClient for managing logins, fetching user inventories, handling trade offers (creating, accepting, and declining), and interacting with the Steam Market to fetch prices and create sell orders.

Tokens
4K
Snippets
18
Records
20
Agent score
21%

What's inside steampy

  1. How SteamGuard authentication works

    master

    Steampy supports two ways to handle SteamGuard:

    1. SteamGuard File: Provide a path to a file containing your credentials. The file should be a JSON object with the following keys:
      • steamid: Your SteamID64
      • shared_secret: Your shared secret
      • identity_secret: Your identity secret
    2. Manual Secrets: Use the guard module functions to generate one-time codes or confirmation keys using the shared_secret and identity_secret directly.
    {
        "steamid": "YOUR_STEAM_ID_64",
        "shared_secret": "YOUR_SHARED_SECRET",
        "identity_secret": "YOUR_IDENTITY_SECRET"
    }
  2. Configure proxies for SteamClient

    master

    You can provide a proxies dictionary during initialization or use set_proxies to configure the session for internal SteamClient requests.

    from steampy.client import SteamClient
    
    # Via constructor
    proxies = {
        "http": "http://login:password@host:port", 
        "https": "http://login:password@host:port"
    }
    steam_client = SteamClient('MY_API_KEY', proxies=proxies)
    
    # Via method
    steam_client.set_proxies(proxies)
  3. Manage trade offers with SteamClient

    master

    The SteamClient provides several methods for interacting with trade offers. Note that most of these require a successful login() call first.

    • get_trade_offers(merge=True, get_sent_offers=True, get_received_offers=True, use_webtoken=False, max_retry=5): Fetches active, non-historical trade offers. If merge=True, item descriptions are merged into the item data.
    • get_trade_offer(trade_offer_id, merge=True, use_webtoken=False): Fetches a specific offer. If use_webtoken is True, it uses an access_token instead of an api_key.
    • get_trade_receipt(trade_id): Retrieves the receipt for a completed trade. Use the tradeid from the offer, not the tradeofferid.
    • make_offer(items_from_me, items_from_them, partner_steam_id, message=''): Creates a trade offer to a friend or Steam. Uses the identity_secret to automatically confirm the trade.
    • make_offer_with_url(items_from_me, items_from_them, trade_offer_url, message='', case_sensitive=True): Similar to make_offer but uses a trade URL, allowing trades with non-friends.
    • accept_trade_offer(trade_offer_id): Automatically accepts an incoming offer using the identity_secret.
    • decline_trade_offer(trade_offer_id): Declines an offer sent to you.
    • cancel_trade_offer(trade_offer_id): Cancels an offer you sent to someone else.
    • get_escrow_duration(trade_offer_url): Checks the escrow duration for a specific partner's trade URL.
  4. Initialize and login to SteamClient

    master

    To use most features, you must initialize SteamClient with an api_key. You can then log in using a username, password, and a path to a SteamGuard file. Alternatively, you can log in using browser cookies or a with statement for automatic login/logout management.

    from steampy.client import SteamClient
    
    # Standard login
    steam_client = SteamClient('MY_API_KEY')
    steam_client.login('MY_USERNAME', 'MY_PASSWORD', 'PATH_TO_STEAMGUARD_FILE')
    
    # Login using cookies
    login_cookies = {} # provide dict with cookies
    steam_client = SteamClient('MY_API_KEY', username='MY_USERNAME', login_cookies=login_cookies)
    
    # Automatic login/logout using context manager
    with SteamClient('MY_API_KEY', 'MY_USERNAME', 'MY_PASSWORD', 'PATH_TO_STEAMGUARD_FILE') as client:
        client.some_method1(...)
  5. Use SteamClient.get_my_inventory and get_partner_inventory

    master

    Retrieve inventory items for a specific game using GameOptions. If merge=True, item descriptions are merged into the item dictionary keyed by item id.

    • get_my_inventory(game, merge=True, count=5000)
    • get_partner_inventory(partner_steam_id, game, merge=True, count=5000)
    from steampy.client import SteamClient, Asset
    from steampy.models import GameOptions
    
    steam_client = SteamClient('MY_API_KEY')
    steam_client.login('MY_USERNAME', 'MY_PASSWORD', 'PATH_TO_STEAMGUARD_FILE')
    
    game = GameOptions.CS
    my_items = steam_client.get_my_inventory(game)
    
    # To create an Asset object for a trade:
    item_id = '7146788981'
    my_asset = Asset(item_id, game)
  6. Manage Steam Market with SteamClient.market

    master

    The market attribute of SteamClient provides tools for market interaction. Most methods require login().

    • fetch_price(item_hash_name, game, currency=Currency.USD): Fetches current market price. Warning: May raise TooManyRequests if called more than 20 times in 60 seconds.
    • fetch_price_history(item_hash_name, game): Returns a list of price history entries. Each entry is [date, price, volume].
    • get_my_market_listings(): Returns your active market listings.
    • create_sell_order(assetid, game, money_to_receive): Creates a sell order. Note: money_to_receive must be in cents (e.g., `
  7. Reference: Currency constants

    master

    The Currency class provides constants for various Steam currencies. Default is Currency.USD.

    # Examples of available currencies
    Currency.USD
    Currency.GBP
    Currency.EURO
    Currency.RUB
    Currency.CNY
    # ... and many others (e.g., JPY, CAD, AUD, etc.)
  8. Reference: Guard module functions

    master

    Functions for handling SteamGuard and authentication codes.

    # load_steam_guard(steam_guard: str) -> dict
    # If steam_guard is a filename, it loads/parses it. Otherwise, parses as JSON string.
    
    # generate_one_time_code(shared_secret: str, timestamp: int = None) -> str
    # Generates a 2FA code for login.
    
    # generate_confirmation_key(identity_secret: str, tag: str, timestamp: int = int(time.time())) -> bytes
    # Generates a mobile device confirmation key for accepting trades.
  9. Reference: Utils methods for price calculation

    master

    Utility functions to calculate market prices including fees.

    from decimal import Decimal
    from steampy.utils import calculate_gross_price, calculate_net_price
    
    # calculate_gross_price(price_net, publisher_fee, steam_fee=Decimal('0.05'))
    # Returns amount buyer pays. Default steam_fee is 5%.
    
    # calculate_net_price(price_gross, publisher_fee, steam_fee=Decimal('0.05'))
    # Returns amount seller receives. Default steam_fee is 5%.
  10. Manage Steam sessions with context managers

    master

    The SteamClient supports the context manager pattern (with statement). Using it ensures that login() is called upon entry and logout() is called upon exit, automatically cleaning up the session.

    from steampy.client import SteamClient
    
    with SteamClient(api_key='...', username='...', password='...', steam_guard='...') as client:
        # Perform authenticated actions
        inventory = client.get_my_inventory(game=my_game_options)
    # Logout is called automatically here
  11. Make a new trade offer

    master

    Create a new trade offer to a partner using make_offer(). You can also use make_offer_with_url() if you have a trade link.

    make_offer parameters:

    • items_from_me (list[Asset]): List of Asset objects you are offering.
    • items_from_them (list[Asset]): List of Asset objects you are requesting.
    • partner_steam_id (str): The SteamID64 of the recipient.
    • message (str): An optional message to include in the trade.

    make_offer_with_url parameters:

    • trade_offer_url (str): The full URL of the trade link.
    • confirm_trade (bool): If True, automatically attempts mobile confirmation if the offer requires it.
    # Making an offer via SteamID
    client.make_offer(items_from_me=[asset1], items_from_them=[asset2], partner_steam_id='7656119...')
    
    # Making an offer via Trade URL
    client.make_offer_with_url(items_from_me=[asset1], items_from_them=[asset2], trade_offer_url='https://steamcommunity.com/tradeoffer/new/...')