Package: buycoins
Buycoins Python LibraryThis library provides easy access to the Buycoins API using the Python programming language. It provides all the feature of the API so that you don't need to interact with the API directly. This library can be used with Python 3.6+Table of ContentsLinksInstallationIntroductionPrimerInitializationAccountsCreate Naira deposit accountOrdersGet cryptocurrency pricesGet price for single cryptocurrencyBuy a cryptocurrencySell a cryptocurrencyP2P TradingPlace limit ordersPlace market orderGet list of ordersGet market bookTransactionsGet cryptocurrency balancesGet single balanceEstimate network feeCreate wallet addressSend cryptocurrencyWebhooksVerify event payloadContributingLicenseLinksBuycoins API documentation:https://developers.buycoins.africa/InstallationYou can install this package using pip:pipinstall--upgradebuycoinsIntroductionPrimerThe library is structured around the concept of atype, so everything is a type.All date quantities are specified as timestamps. So you would have to reconstruct the ISO dates yourself if you ever need to.All cryptocurrency (and monetary) values are specified as decimals.Supports all cryptocurrencies supported by BuycoinsInitializationFirstly, request API access by sending an email tosupport@buycoins.africawith the email address you used in creating a Buycoins account.
When you've been granted access, you should be able to generate a public and secret key from the 'API settings' section of your account.You have to initialize the library once in your app. You can do this when initializing databases, logging, etc.
As a security practice, it is best not to hardcode your API keys. You should store them in environmental variables or a remote Secrets Manager.importbuycoinsbuycoins.initialize('<PUBLIC-KEY>','<SECRET-KEY>')AccountsAccounts provide a way to programmatically create virtual naira accounts.TypesVirtualDepositAccountType:account_number:straccount_name:straccount_type:strbank_name:straccount_reference:strCreate Naira deposit accountimportbuycoinsasbcacc=bc.accounts.create_deposit('john doe')# VirtualDepositAccountTypeprint(acc.account_name)# john doeprint(acc.bank_name)# bank nameprint(acc.account_number)# account numberprint(acc.account_reference)# account referenceprint(acc.account_type)# account typeOrdersOrders provide a way to buy from and sell directly to Buycoins.
When buying or selling, price ID should be the ID gotten from calling either.get_price()or.get_prices().
Make sure to use price that hasn't expired yet, so call.get_price(cryptocurrency)to get the latest price for the cryptocurrency just before buying or selling.TypesCoinPriceType:id:strcryptocurrency:strbuy_price_per_coin:Decimalmin_buy:Decimalmax_buy:Decimalexpires_at:intclassOrderType(NamedTuple):id:strcryptocurrency:strstatus:strside:strcreated_at:inttotal_coin_amount:strstatic_price:Decimalprice_type:strdynamic_exchange_rate:strcoin_amount:DecimalGet cryptocurrency pricesimportbuycoinsasbc# Get prices of all cryptocurrenciesprices=bc.orders.get_prices()# CoinPriceType[]print(prices[0].id)# ID of first price entryprint(prices[0].cryptocurrency)# cryptocurrencyprint(prices[0].expires_at)# when this price entry will expireprint(prices[0].buy_price_per_coin)# coin priceprint(prices[0].min_buy)# minimum amount you can buyprint(prices[0].max_buy)# max amount you can buyGet single cryptocurrency priceimportbuycoinsasbcprice=bc.orders.get_price('bitcoin')# CoinPriceTypeprint(price.id)# ID of price entryprint(price.cryptocurrency)# cryptocurrencyprint(price.expires_at)# when this price entry will expireprint(price.buy_price_per_coin)# coin priceprint(price.min_buy)# minimum amount you can buyprint(price.max_buy)# max amount you can buyBuy a cryptocurrencyimportbuycoinsasbcorder=bc.orders.buy(price_id='price-id',coin_amount=1.52,cryptocurrency='litecoin')# OrderTypeprint(order.id)# Order IDprint(order.status)# either active or inactiveprint(order.side)# buyprint(order.cryptocurrency)# litecoinprint(order.total_coin_amount)# Total coin amountprint(order.price_type)# Price typeSell a cryptocurrencyimportbuycoinsasbcorder=bc.orders.sell(price_id='price-id',coin_amount=0.0043,cryptocurrency='ethereum')# OrderTypeprint(order.id)# Order IDprint(order.status)# either active or inactiveprint(order.side)# sellprint(order.cryptocurrency)# litecoinprint(order.total_coin_amount)# Total coin amountprint(order.price_type)# Price typeP2P TradingP2P Trading lets you trade cryptocurrencies with other users.
If you are not familiar with p2p trading on the Buycoins platform, read about ithereTypesclassOrderType(NamedTuple):id:strcryptocurrency:strstatus:strside:strcreated_at:inttotal_coin_amount:strstatic_price:Decimalprice_type:strdynamic_exchange_rate:strcoin_amount:DecimalPlace limit ordersWhen placing limit orders, ifprice_typeisstatic,static_pricemust also be specified, and ifprice_typeisdynamic,dynamic_exchange_ratemust be provided.importbuycoinsasbcorder=bc.p2p.place_limit_order(side='buy',# either 'buy' or 'sell'coin_amount=0.00043,cryptocurrency='ethereum',price_type='static',static_price=0.004,dynamic_exchange_rate=None# float)# OrderTypeprint(order.id)# Order IDprint(order.status)# status, either active or inactiveprint(order.cryptocurrency)# bitcoin, litecoin, etcprint(order.coin_amount)# coin amountPlace market ordersimportbuycoinsasbc# Place market order# `order` has all the properties as shown aboveorder=bc.p2p.place_market_order(side='buy',# either buy or sellcoin_amount=0.00023,cryptocurrency='litecoin')# order is an OrderTypeprint(order.id)# Order IDprint(order.status)# status, either active or inactiveprint(order.cryptocurrency)# bitcoin, litecoin, etcprint(order.coin_amount)# coin amountGet list of ordersimportbuycoinsasbcorders,dynamic_price_expiry=bc.p2p.get_orders('active')# (OrderType[], timestamp)print(dynamic_price_expiry)# timestamp of when dynamic price expiresprint(orders[0].id)# ID of first orderprint(orders[1].status)# status of the first orderGet Market bookimportbuycoinsasbcmarket_book,dynamic_price_expiry=bc.p2p.get_market_book()# (OrderType[], timestamp)print(dynamic_price_expiry)# timestamp of when dynamic price expiresprint(market_book[0].id)# ID of first orderprint(market_book[1].status)# status of the first orderTransactionsTransactions enable you to send and receive cryptocurrencies.TypesCoinBalanceType:id:strcryptocurrency:strconfirmed_balance:DecimalNetworkFeeType:estimated_fee:Decimaltotal:DecimalTransactionType:hash:strid:strSendReturnValueType:id:straddress:strcryptocurrency:stramount:Decimalfee:Decimalstatus:strtransaction:TransactionTypeAddressType:cryptocurrency:straddress:strGet balancesimportbuycoinsasbcbalances=bc.transactions.get_balances()# CoinBalanceType[]print(balances[0].cryptocurrency)# bitcoin, litecoin, etcprint(balances[0].confirmed_balance)# the confirmed balanceGet single balanceimportbuycoinsasbcbalance=bc.transactions.get_balance('bitcoin')# CoinBalanceTypeprint(balance.cryptocurrency)# bitcoinprint(balance.confirmed_balance)# the confirmed balanceEstimate network feeimportbuycoinsasbcfee=bc.transactions.estimate_network_fee('bitcoin',# cryptocurrency0.32,# txn amount)# NetworkFeeTypeprint(fee.estimated_fee)# estimated fee for txnprint(fee.total)# totalSend cryptocurrency to an addressimportbuycoinsasbcsent=bc.transactions.send(cryptocurrency='ethereum',amount=0.0023,address='<wallet-address>')# SendReturnValueTypeprint(sent.fee)# fee charged for the 'send' txnprint(sent.status)# status of the txnprint(sent.transaction.id)# ID of the txnprint(sent.transaction.hash)# txn hashCreate wallet addressimportbuycoinsasbcaddr=bc.transactions.create_address('bitcoin')# AddressTypeprint(addr.address)# Address stringprint(addr.cryptocurrency)# cryptocurrencyWebhooksWebhooks provides a way for Buycoins to inform you of events that take place on your account.
See theBuycoins documentationfor  an introduction and the available events.Verify event payloadEnsure that the webhook event originated from Buycoinsimportbuycoinsasbcis_valid=bc.webhook.verify_payload(body='<raw request body from buycoins (in bytes)>',webhook_token='<webhook-token generated on buycoins>',header_signature='X-Webhook-Signature header')print(is_valid)# True if the event is from Buycoins, False otherwise.TestingTo run tests:poetryrunpytestContributingSeeCONTRIBUTING.mdLicenseMIT License