[Python] Connecting to Coinbase Sandbox API using

Discussão em 'Python' iniciado por Stack, Janeiro 24, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to use Coinbase in Sandbox mode to try out a strategy.

    I created an Sandbox API using the instructions here https://docs.cdp.coinbase.com/exchange/docs/sandbox/

    However, when I use them in a Python script, they don't work. It returns an error.

    I am using a simple script

    from coinbase.wallet.client import Client
    import os
    from dotenv import load_dotenv

    # Load environment variables from a .env file
    load_dotenv()

    # Your API credentials from Coinbase
    API_KEY = os.getenv("API_KEY")
    API_SECRET = os.getenv("API_SECRET")

    # Initialize the Coinbase client
    client = Client(API_KEY, API_SECRET)

    def get_cash_balance():
    # Get accounts
    accounts = client.get_accounts()

    # Find and print the cash balances
    for account in accounts.data:
    if account['type'] == 'fiat':
    print(f"Currency: {account['currency']}")
    print(f"Balance: {account['balance']['amount']}{account['balance']['currency']}")

    if __name__ == "__main__":
    get_cash_balance()


    It gives me an error ending with

    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
    requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)


    Am I doing something wrong? How can I fix this? My keys are exactly as they were on the site.

    I even tried using curl

    curl -X GET https://api.coinbase.com/v2/accounts -u "API_KEY:API_SECRET"


    The response from that is Unauthorized

    Continue reading...

Compartilhe esta Página