TM
Blog

Getting Started with the Stripe API for Python

Hoss@@Hossapp /

Getting Started with the Stripe API for Python

In this guide, we will show you how to get up and running with the Stripe API using Python. We will cover the basics of making a cardholder, creating a card, and finally issuing a charge.

Installing and Importing Libraries

Before you start using the API, you will need to install a few libraries. This can be done via the pip package manager. We will be using Hoss to monitor our calls and analyze the information we send in and the information we receive. If you haven’t signed up for it, you can do so here.

Using pip, install the Hoss Agent, requests library, and the Stripe python client library.

$ pip install requests
$ pip install hoss_agent
$ pip install stripe

After installing these libraries, open up your preferred developer environment and import the following libraries. We imported “requests” to assist us in making API calls, “hoss_agent” to allow us to use Hoss’s service, and “stripe” to make calls to the Stripe API.

import requests
import hoss_agent
import stripe

Integrate the Hoss SDK

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

We have requested to view the client variable to double check our client in our Jupyter notebook.

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

hoss_key ="development-#################################"
client = hoss_agent.init({"API_KEY": hoss_key})

Getting Started With Stripe

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

Making your First Request

Notice the stripe.apikey = "sk\test_####################" line. This line passes your API key into the client. This is necessary - otherwise you will not be able to make successful API requests.

In this example, we are creating an issuing cardholder and inputting their billing information. Once we make this, we can see the notebook outputting the JSON for this new cardholder.

stripe.api_key = "sk_test_####################"

stripe.issuing.Cardholder.create(
  type="individual",
  name="Eggs Benedict",
  email="fresheggs@example.com",
  phone_number="+18888675309",
  billing={
    "address": {
      "line1": "1234 5th Ave",
      "city": "San Francisco",
      "state": "CA",
      "country": "US",
      "postal_code": "94111",
    },
  },
)

This is the response returned

<Cardholder issuing.cardholder id=ich_1H8wko2eZvKYlo2C9UrQ93GU at 0x7f4eda7bc290> JSON: {
  "billing": {
    "address": {
      "city": "San Francisco",
      "country": "US",
      "line1": "1234 5th Ave",
      "line2": null,
      "postal_code": "94111",
      "state": "CA"
    }
  },
  "company": null,
  "created": 1595719570,
  "email": "fresheggs@example.com",
 
 
}

Creating a Card

We’re going to first create a card.

our_card = stripe.Customer.create_source(
  "cus_Hi9njHX2f8Rf8F",
  source="tok_amex",
)

our_card

This is the response returned:

<Card card id=card_1H9C7y2eZvKYlo2CDeUkNxnw at 0x7fe33a7a32b0> JSON: {
  "address_city": null,
  "address_country": null,
  "address_line1": null,
  "address_line1_check": null,
  "address_line2": null,
  "address_state": null,
  "address_zip": null,
  "address_zip_check": null,
  "brand": "American Express",

  
}

Creating your First Charge

Now, we are going to create a charge. This is how you charge a credit card (or your preferred payment source). Since we are only in test mode, we won’t actually be charged.

stripe.Charge.create(
  amount=2000,
  currency="usd",
  source="tok_mastercard",
  description="My First Test Charge (created for API docs)",
)

This is the response returned.

<Charge charge id=ch_1H9C802eZvKYlo2CC9EEsFxC at 0x7fe33a7a3258> JSON: {
  "amount": 2000,
  "amount_refunded": 0,
  "application": null,
  "application_fee": null,
  "application_fee_amount": null,
  "balance_transaction": "txn_1H9C802eZvKYlo2CsUcx0Jam",

 
}

Monitoring with Hoss

View your Request History in Hoss

On the toolbar on the left side of the panel, you can select which API you want to view and then select “Requests”. On this page, you can also view important metrics about API performance, such as latency. We can see the Stripe API requests - clicking on the individual requests allows us to examine the headers, payloads, and other relevant information about the request and response.

Set Up Alarms

You can set up alarms to monitor API status, failed calls, and latency. We integrate with Slack and Pagerduty - allowing you to send notifications directly to the folks responsible for working with these API integrations.

Masking Fields

It should be noted that when developing with the Stripe 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 Stripe API documentation here and get started building your application!

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

Using the Quovo API with Python

Hoss
Read more
Today, we will show you how to use the Quovo API with Python. We'll cover setup, authentication, and making basic requests.

Using the Quovo API with Ruby

Hoss
Read more
Today, we’re going to show you how to get started with the Quovo API in Ruby. We’ll cover library installation, obtaining an access token, and making some basic requests.
TM

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