Package: coinbase-commerce
coinbase-commerce-pythonCoinbase Commerce PythonTable of contentsPython VersionsThird Party Libraries and DependenciesDocumentationInstallationUsageCheckoutsChargesEventsValidating webhook signaturesTesting and ContributingPython VersionPython 2.7+ and 3.4+ are supported and tested.Third Party Libraries and DependenciesThe following libraries will be installed when you install the client library:requestsDocumentationFor more details visitCoinbase API docs.To start using library, you need to register onCommerce SignUp.
And get yourAPI_KEYfrom user settings.Next create aClientobject for interacting with the API:fromcoinbase_commerce.clientimportClientAPI_KEY="API KEY"client=Client(api_key=API_KEY)Clientcontains links to an every Python Class representations of the API resourcesCheckout, Charge, EventYou can calllist, list_paging_iter, create, retrieve, modifymethods from an API resource classesclient.charge.createclient.checkout.list_paging_iterclient.event.listclient.charge.retrieveclient.checkout.modifyas well assave, delete, refreshmethods from API resource class instances.checkout=client.checkout.retrieve(<id>)checkout.refresh()checkout.save()checkout.delete()Each API method returns anAPIObject(a subclass ofdict) representing the JSON response from the API, all of the models are dumpable with JSON.Also when the response data is parsed into Python objects, the appropriateAPIObjectsubclasses will be used automatically.
All subclasses ofAPIResourceclass supportrefreshmethod. This will update their attributes and all nested data by making a freshGETrequest to the relevant API endpoint.Client support Common API Errors and Warnings handling.
All errors occuring during interaction with the API will be raised as exceptions.ErrorStatus CodeAPIError*InvalidRequestError400ParamRequiredError400ValidationError400AuthenticationError401ResourceNotFoundError404RateLimitExceededError429InternalServerError500ServiceUnavailableError503InstallationInstall withpip:pip install --upgrade coinbase-commerceor witheasy_install:easy_install --upgrade coinbase-commerceUsagefromcoinbase_commerce.clientimportClientclient=Client(api_key=API_KEY)CheckoutsCheckouts API docsRetrievecheckout=client.checkout.retrieve(<checkout_id>)Createcheckout_info={"name":'The Sovereign Individual',"description":'Mastering the Transition to the Information Age',"pricing_type":'fixed_price',"local_price":{"amount":"100.00","currency":"USD"},"requested_info":["name","email"]}checkout=client.checkout.create(**checkout_info)# orcheckout=client.checkout.create(name='The Sovereign Individual',description='Mastering the Transition to the Information Age',pricing_type='fixed_price',local_price={"amount":"100.00","currency":"USD"},requested_info=["name","email"])Updatecheckout=client.checkout.retrieve(<checkout_id>)checkout.name='new name'checkout.save()# orcheckout=client.checkout.modify('<checkout_id>',name='new name')Deletecheckout.delete()Listcheckouts=client.checkout.list()Paging list iterationsforcheckoutinclient.checkout.list_paging_iter():print("{!r}".format(checkout))ChargesCharges API docsRetrievecharge=client.charge.retrieve(<charge_id>)Createcharge_info={"name":"The Sovereign Individual","description":"Mastering the Transition to the Information Age","local_price":{"amount":"100.00","currency":"USD"},"pricing_type":"fixed_price"}charge=client.charge.create(**charge_info)# orcharge=client.charge.create(name='The Sovereign Individual',description='Mastering the Transition to the Information Age',pricing_type='fixed_price',local_price={"amount":"100.00","currency":"USD"})Listcheckouts=client.charge.list()Paging list iterationsforchargeinclient.charge.list_paging_iter():print("{!r}".format(charge))EventsEvents API DocsRetrieveevent=client.event.retrieve(<event_id>)Listevents=client.event.list()Paging list iterationsforeventinclient.event.list_paging_iter():print("{!r}".format(event))Validating webhook signaturesYou could verify webhook signatures using our library.
To perform the verification you'll need to provide the event data, a webhook signature from request header, and the endpoint’s secret.
In case of invalid request signature or request payload, you will receive appropriate error message.WEBHOOK_SECRET='your_webhook_secret'# using Flask@app.route('/webhooks',methods=['POST'])defwebhooks():# event payloadrequest_data=request.data.decode('utf-8')# webhook signaturerequest_sig=request.headers.get('X-CC-Webhook-Signature',None)try:# signature verification and event object constructionevent=Webhook.construct_event(request_data,request_sig,WEBHOOK_SECRET)except(WebhookInvalidPayload,SignatureVerificationError)ase:returnstr(e),400print("Received event: id={id}, type={type}".format(id=event.id,type=event.type))return'success',200Testing and ContributingAny and all contributions are welcome! The process is simple: fork this repo, make your changes, run the test suite, and submit a pull request. Tests are run via nosetest. To run the tests, clone the repository and then:Install the requirementspip install -r requirements.txtRun the tests for your current version of PythonUse tox to run the test suite against multiple versions of Python. You can install tox with pip or easy_install:pip install tox
easy_install toxTox requires the appropriate Python interpreters to run the tests in different environments. We recommend using pyenv for this. Once you've installed the appropriate interpreters, running the tests in every environment is simple:tox