TM
Blog

Using the Square Payments API with Python

Hoss@@Hossapp /

In this tutorial you will learn how to use Square’s Payments API to make a sample payment using Python.

Installing Libraries

We will require some libraries to be installed for us to proceed. We will use the pip package manager to install. We will be using Hoss to monitor our API calls and make sure that they execute correctly. Check Hoss out here.

$ pip install squareup
$ pip install hoss
$ pip install requests

Lets import the required libraries for our program to work.

import os
import sys
import requests
import hoss_agent
import json

Integrate the Hoss SDK

Here, we integrated the Hoss SDK. It's as simple as writing these two lines!

You can find your Hoss keys in your settings here.There are three keys for development, production, and staging respectively. We are using our development key here.

hoss_key ='development-##################################'
hoss_client = hoss_agent.init(hoss_key)

Getting Started With Square

Now, go over to your Square account, and note down your Square API key. If you haven’t done so already, you will need to create a Square Developer account. You can find that once you have logged in to your Square account here.

Making your First Request

The first step in making a request is creating a client to make requests. This is a functionality provided by the imported squareup library.

To create the client object, you will need to pass in your access token, which can be found in your developer portal. Keep in mind that we’ll be working in the sandbox environment, since we are simply demonstrating some requests, not building an actual application in this tutorial.

# setup client for Square, inputting our access token
# Environment is set to sandbox for us to test
client = Client(
    access_token='#####################################',
    environment='sandbox'
)

Now, we make our first request. We will work with the “create payment” endpoint, choosing to make a payment of 200 US Dollars. We will store the response in the “result” variable.

# result body structure set
result = client.payments.create_payment(
  body = {
    "source_id": "cnon:card-nonce-ok",
    "idempotency_key": "6b899430-fe20-4fed-b81d-f7bd60d80634",
    "amount_money": {
           "amount": 200,
           "currency": "USD"
    }
  }
)

if result.is_success():
    print(result.body)
elif result.is_error():
    print(result.errors)

The successful response, curtailed here for readability reasons, provides details of the transaction, including information about the payment source.

{
  "payment": {
    "id": "GQTFp1ZlXdpoW4o6eGiZhbjosiDFf",
    "created_at": "2019-07-10T13:23:49.154Z",
    "updated_at": "2019-07-10T13:23:49.446Z",
    "amount_money": {
      "amount": 200,
      "currency": "USD"
    },
    "app_fee_money": {
      "amount": 10,
      "currency": "USD"
    },
    
    
  }
}

Monitoring with Hoss

View your Request History in Hoss

On the left hand side of the page, you can select the desired API to see metrics about. On the individual API’s page, you will also see the calls you have made, and clicking on those will then show you data like headers and response bodies.

Set Up Alarms

With Hoss, you can set up alarms to help you monitor your APIs, ranging from failed calls to latency alerts. We even integrate with Slack and Pagerduty, so you can always be in the know regarding your API integrations.

Masking Fields

It should be noted that when developing with the Square API, you will receive personally identifiable information with the API responses. If you’re using the Hoss app to monitor your API usage, you may want to read more about masking here.

Next Steps

That’s it! You can access the full Square API documentation here and get started building your application!

Subscribe to Hoss ModeA weekly newsletter with curated articles, community discussions, news and trends.

Getting Started with the Stripe API for Ruby

Hoss
Read more
In this guide, we will be using Ruby to integrate Stripe and go from creating a cardholder to making a charge, all while having the ability to monitor and analyze our calls.
TM

Copyright © Hoss Technologies, Inc. 2020 - All rights reserved. Terms of Service & Privacy Policy