简体   繁体   中英

Retrieving "amount" from primary account using Coinbase API

I'm very new to APIs so this question might be super obvious to most. Using python I am trying to retrieve my Bitcoin balance in BTC, so that I can make a bot that detects how much I have in my Bitcoin wallet and sell a percentage of that value. However, I am having difficulties actually retrieving the amount of BTC in my primary account. I am able to find my alt coin balances using these lines:

accounts = client.get_accounts() for wallet in accounts.data: print(str(wallet['name'])+str(wallet['native_balance']))

However, I am not quite sure how to retrieve the amount value from my primary account, and BTC doesn't show up in this set when I run it. on coinbases api dev site, this is how it says the data is setup:

  {
  "pagination": {
    "ending_before": null,
    "starting_after": null,
    "limit": 25,
    "order": "desc",
    "previous_uri": null,
    "next_uri": null
  },
  "data": [
    {
      "id": "58542935-67b5-56e1-a3f9-42686e07fa40",
      "name": "My Vault",
      "primary": false,
      "type": "vault",
      "currency": "BTC",
      "balance": {
        "amount": "4.00000000",
        "currency": "BTC"
      },
      "created_at": "2015-01-31T20:49:02Z",
      "updated_at": "2015-01-31T20:49:02Z",
      "resource": "account",
      "resource_path": "/v2/accounts/58542935-67b5-56e1-a3f9-42686e07fa40",
      "ready": true
    },
    {
      "id": "2bbf394c-193b-5b2a-9155-3b4732659ede",
      "name": "My Wallet",
      "primary": true,
      "type": "wallet",
      "currency": "BTC",
      "balance": {
        "amount": "39.59000000",
        "currency": "BTC"
      },
      "created_at": "2015-01-31T20:49:02Z",
      "updated_at": "2015-01-31T20:49:02Z",
      "resource": "account",
      "resource_path": "/v2/accounts/2bbf394c-193b-5b2a-9155-3b4732659ede"
    }
  ]
}

first thought was to figure out how to reverse engineer what makes the lines work for the wallets work, but everything I've tried hasn't worked, so clearly I am just unaware of how get_primary_account() works

All I want is "amount" which is within "data", and to be able to convert that to an float. Any help?

Well I found the solution. Doubt anyone else will have the same issue I did as mine was just not understanding API's very well, but to retrieve my balance for my primary account I simply had to type the following:

  primacct=client.get_primary_account()
  btcBal=str(primacct['balance'])

I was overcomplicating things for no reason.. Hopefully this also clears up what my question was in the first place to those who read it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM