Getting Started With Stripe API

Getting Started With Stripe API

Posted in

When you’re dealing with money, you need to know your API is safe and secure. You don’t want to be like the Russian Central Bank, who lost $31 million due to data breaches in 2016. Or, you don’t want to follow Binance’s example, either, who in 2022 lost over two million BNB tokens in a hack, an equivalent of $570 million in lost assets.

If you’re handling finances or payments, you need to be able to trust your payment API. Stripe is one of the industry standards thanks to its ease-of-use, customizability, pricing transparency, and robust security. It’s the payment API of choice for everyone from Amazon to Instacart.

We’re going to show you how to get started using Stripe API. We’ll show you how to sign up for an account, request API keys, and make your first API call using the Stripe Shell.

Getting Started With Stripe API

Sign Up For A Stripe Account

Before starting, you’ll need a Stripe account if you don’t already have one. To begin, navigate to the Stripe documentation page and find the Sign In button in the top-right corner. After you’ve created your account, you’ll be given two API keys, a client-side key that can be published and a server-side key that needs to be kept secret.

Keep in mind you’ll need to activate your Stripe account before you can test out the Stripe API.

Open Stripe Shell

Stripe has its own CLI, the Stripe Shell, which lets you run Stripe CLI commands directly from the docs page. Commands sent from Stripe Shell are automatically run in Test Mode, so you don’t have to worry about moving any money.

To open Stripe Shell, navigate to the Stripe Docs and find the prompt in the bottom-right corner. Open it. This gives you access to the Strip Shell CLI.

Send Your First API Request

From the command line, enter:

stripe customers create --email=jane.smith@email.com --name="Jane Smith" --description="My First Stripe Customer"

This should return the following:

{
  "id": "cus_NNikl3nUrNLVxz",
  "object": "customer",
  "address": null,
  "balance": 0,
  "created": 1676753236,
  "currency": null,
  "default_source": null,
  "delinquent": false,
  "description": "My First Stripe Customer",
  "discount": null,
  "email": "jane.smith@email.com",
  "invoice_prefix": "A4DC87B2",
  "invoice_settings": {
    "custom_fields": null,
    "default_payment_method": null,
    "footer": null,
    "rendering_options": null
  },
  "livemode": false,
  "metadata": {},
  "name": "Jane Smith",
  "next_invoice_sequence": 1,
  "phone": null,
  "preferred_locales": [],
  "shipping": null,
  "tax_exempt": "none",
  "test_clock": null
}

As you can see, this Stripe API query assigned “jane.smith@email.com” to email, “Jane Smith” to name, and added the description “My First Stripe Customer.” A unique ID and invoice number are generated.

If you want to try this command in test mode, input the following:

stripe customers create --email=jane.smith@email.com --name="Jane Smith" --description="My First Stripe Customer" --api-key sk_test_51McwhlFiWVLfrL2izRqeLQLKd4pLEIbzf83rm9GNqkgoKJvKt1JV4MnaCJKRr1LY3HYKLL1jbS0vIeDZbpWnd2ff00rKnc12zQ

If everything is correct, you should get a result like this:

{
  "id": "cus_LfdZgLFhah76qf",
  "object": "customer",
  "address": null,
  "balance": 0,
  "created": 1652286103,
  "currency": null,
  "default_currency": null,
  "default_source": null,
  "delinquent": false,
  "description": "My First Stripe Customer",
  "discount": null,
  "email": "jane.smith@email.com",
  "invoice_prefix": "D337F99E",
  "invoice_settings": {
    "custom_fields": null,
    "default_payment_method": null,
    "footer": null
  },
  "livemode": false,
  "metadata": {
  },
  "name": "Jane Smith",
  "next_invoice_sequence": 1,
  "phone": null,
  "preferred_locales": [

  ],
  "shipping": null,
  "tax_exempt": "none",
  "test_clock": null
}

Check Events and Logs

The Stripe API logs every event and API call associated with your account. You can view the events and API queries associated with either the test or live account, depending on which API key you supply.

To see the API log:

  • Open the site for Logs
  • Select 200 OK Post /v1 customers

To see the events:

  • Open the site for Events
  • Select the entry for jane.smith@gmail.com as a new customer.

This will reveal all of the data associated with an event in JSON format.

Store Your API Key

The Stripe API has four different API keys. One is the Secret Key for Test Mode. Another is Test Mode’s publishable key. Then Live Mode has both secret and publishable API keys, as well.

You’ll want to make sure you have those stored someplace secure. You won’t need to reference them that often when you’re logged into your Stripe account, though, as the site automatically populates the API key into your code when you’re logged in.

Finally, let’s take a look at the Stripe API in action to help give you some ideas of its capabilities. We’re going to create a one-time payment link so you can see the Stripe API in action.

To start, you need to create a Product as Products and Prices are what Stripe API needs to function. To create a new Product from the command line, input:

curl https://api.stripe.com/v1/products \
  -u YOUR_API_KEY
  -d name="Gold Special"

Insert the API key you were given for testing earlier after the -u modifier. Hit enter.

You should see a result like the following:

{
  "id": "prod_NNkp1h3kpSz11k",
  "object": "product",
  "active": true,
  "attributes": [],
  "created": 1676760930,
  "default_price": null,
  "description": null,
  "images": [],
  "livemode": false,
  "metadata": {},
  "name": "Gold Special",
  "package_dimensions": null,
  "shippable": null,
  "statement_descriptor": null,
  "tax_code": null,
  "type": "service",
  "unit_label": null,
  "updated": 1676760930,
  "url": null
}

As you can see, a product named Gold Special was created and given a unique identifier. Now we’re going to assign that product a price. Input the following:

curl https://api.stripe.com/v1/prices \
  -u YOUR_API_KEY
  -d unit_amount=2000 \
  -d currency=usd \
  -d "recurring[interval]"=month \
  -d product=prod_NNkp1h3kpSz11k

This assigns a price of $2,000 USD to the item associated with prod_NNkp1h3kpSz11k and specifies it as a recurring charge, happening once a month.

Keep in mind that Stripe one-time payment links only support standard pricing or name-your-own-price. It doesn’t support more advanced options like bulk discounts or graduated pricing, so keep that in mind when creating your links.

Now you’re going to create the payment link. You need to pass in a line item and a price. Input the following:

curl https://api.stripe.com/v1/payment_links \
  -u YOUR_API_KEY: \
  -d "line_items[0][price]"=price_1MczSuFiWVLfrL2iuMTq6KrR \
  -d "line_items[0][quantity]"=1
curl https://api.stripe.com/v1/payment_links \
  -u sk_test_51McwhlFiWVLfrL2izRqeLQLKd4pLEIbzf83rm9GNqkgoKJvKt1JV4MnaCJKRr1LY3HYKLL1jbS0vIeDZbpWnd2ff00rKnc12zQ: \
  -d "line_items[0][price]"=price_1MczSuFiWVLfrL2iuMTq6KrR \
  -d "line_items[0][quantity]"=1

Hit enter, and you’ll see a response similar to this:

{
  "id": "plink_1McznmFiWVLfrL2iilpoJOvo",
  "object": "payment_link",
  "active": true,
  "after_completion": {
    "hosted_confirmation": {
      "custom_message": null
    },
    "type": "hosted_confirmation"
  },
  "allow_promotion_codes": false,
  "application_fee_amount": null,
  "application_fee_percent": null,
  "automatic_tax": {
    "enabled": false
  },
  "billing_address_collection": "auto",
  "consent_collection": null,
  "currency": "usd",
  "custom_fields": [],
  "custom_text": {
    "shipping_address": null,
    "submit": null
  },
  "customer_creation": "if_required",
  "invoice_creation": {
    "enabled": false,
    "invoice_data": {
      "account_tax_ids": null,
      "custom_fields": null,
      "description": null,
      "footer": null,
      "metadata": {},
      "rendering_options": null
    }
  },
  "livemode": false,
  "metadata": {},
  "on_behalf_of": null,
  "payment_intent_data": null,
  "payment_method_collection": "always",
  "payment_method_types": null,
  "phone_number_collection": {
    "enabled": false
  },
  "shipping_address_collection": null,
  "shipping_options": [],
  "submit_type": "auto",
  "subscription_data": {
    "description": null,
    "trial_period_days": null
  },
  "tax_id_collection": {
    "enabled": false
  },
  "transfer_data": null,
  "url": "https://buy.stripe.com/test_28o7u4eQMcaUgWk8ww"
}

Enter the URL into your browser, and you should see a test example of your payment link, with the name of the product and its associated cost.

That’s all it takes! Now, you’re a little more familiar with Stripe API.

Final Thoughts on Getting Started With Stripe API

Once you get comfortable working with the Stripe API, you can easily create your own custom eCommerce sites and shopping carts, send customized invoices, set up recurring payments or subscriptions, and even setup advanced payment solutions like creating physical spending cards, all while maintaining your own unique branding and visual identity.

Even better, Stripe works with a number of other popular retail and eCommerce apps and APIs like Docusign and Mailchimp. Once you get familiar with working with Stripe API, you can have complete control over your entire payments portal, giving you as much control and independence as possible over your business.