Archetype | The Basics of the Python SDK
logo

The Basics of the Python SDK

Wed Aug 17 2022

In the last couple of blogs we discussed the different ways we can monetize our API. In this blog we’ll go over some of the implementation specifics and how we can integrate this with our app. In this post we’ll specifically focus on working with the Python SDK.

Initializing SDK

Now that we have some context, let’s go ahead and install the python SDK.

We have the python package published to pypi.org, under the name archetypewsgi.

pip install archetypewsgi


Note: If we have python3 installed, use pip3 instead.

Verify the package has been installed

$piplist| grep archetypewsgi

archetypewsgi 1.0.3

Create a file called app.py and initialize the python SDK by importing it.

from main import archetypewsgi

archetype = archetypewsgi.Archetype( app_id=“...“, secret_key=“...“ )

archetype = Archetype(settings=settings)

We need to change the values for the following:

  • app_id
  • secret_key

We can get this info from https://app.archetype.dev/settings

Create a Product

Before we can create a User, we need to create a product that could be assigned to the user.

To create a product:


There are several different types of products to choose from, in this blog create a simple subscription type product to understand the basics.

  • Name: This is the name of the product that will show up in the UI
  • Description: Write a short description of the product.
  • Pricing Model: The available options are Subscription, Tiered and Graduated. In this blog we will choose Subscription. For more info on the other models, take a look at docs.archetype.dev.
  • Price: The base flat price of the subscription.
  • Billing Period: Monthly or Yearly.
  • Trial: We can choose the length and whether it's Days, Weeks or Months. We’ll choose 7 Days in this example.

Click on Create to create the product. Next step is to assign this newly created product to an individual user.

Create a User

We should already have Python SDK initialized, to that add the following to create a user:

uid = “ajtest“

Creates a new user

print(archetype.user(uid=uid))

Next step, to understand how we verify.

Well there are one of two ways:

  • In app.py we can fetch the user using the following function

uid = “ajtest“

print(archetype.get_user_details(uid=uid))

The full script would look something like this:

from archetypewsgi import Archetype

settings = { “app_id“: “...“, “secret_key“: “...“, } archetype = Archetype(settings=settings)

uid = “ajtest“

Creates a new user

print(archetype.user(uid=uid))

Get user details

print(archetype.get_user_details(uid=uid))

Flask App

Okay, this is wonderful, but how can we integrate this with our app? Great question! We’ll see how we can use the Archetype python SDK to integrate this with our own app.

In our app.py, the base scaffolding would look similar to when we initialized it previously, with some specific Flask bits.

from flask import Flask, jsonify, request
from archetypewsgi import Archetype

app = Flask(name)

settings = {
    “app_id“: “...“,
    “secret_key“: “...“,
}

archetype = Archetype(settings=settings)

app.wsgi_app = archetype.middleware(app=app.wsgi_app)

Again we can get the app_id and secret_key from https://app.archetype.dev/settings

Using this base scaffold lets build 2 routes:

  • /
@app.route(“/“, methods=[“GET“, “POST“])
def hello_world():
    return “Hello World“
  • /products
@app.route(“/products“)
def get_products():
    return jsonify(archetype.get_all_products(as_json=True))

We’ll launch the app on port 3000.

if name == “main“:
    app.run(port=3000)

The final app.py should look something like this:

from flask import Flask, jsonify, request
from archetypewsgi import Archetype

app = Flask(name)

settings = {
    “app_id“: “...“,
    “secret_key“: “...“,
}

archetype = Archetype(settings=settings)

app.wsgi_app = archetype.middleware(app=app.wsgi_app)

@app.route(“/“, methods=[“GET“, “POST“])
def hello_world():
    return “Hello World“

@app.route(“/products“)
def get_products():
    return jsonify(archetype.get_all_products(as_json=True))

if name == “main“:
    app.run(port=3000)

Start the flask app which should be running on port http://127.0.0.1:3000

$ python3 app.py

  • Serving Flask app 'app' (lazy loading)
  • Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
  • Debug mode: off
  • Running on http://127.0.0.1:3000 (Press CTRL+C to quit)

Let's curl one of the routes and try to get some responses. But before we do that, we need to pass the API Key in the header of the request. Ok, but where do we get the API key from?

We have all the required pieces, let's make a curl call:

curl --header “apikey: ...“ http://127.0.0.1:3000/

Hello World

Now, let’s try to call /products:

curl --header “apikey: ...“ http://127.0.0.1:3000/products

[

{

"currency": "usd",

"description": "AJ Acct 2",

"has_full_access": true,

"has_quota": false,

"has_trial": false,

"is_active": true,

"is_free": false,

"is_private": false,

"live_mode": false,

"name": "AJ Acct 2",

"period": "month",

"price": 15,

"quota": null,

"rate_limit": 100,

"tier_id": "prod_M0ivDFxe9xTtvj",

"trial_length": 0,

"trial_time_frame": "day",

"usage_tiers": [],

"usage_type": "licensed",

"version_number": 1

  
}

]

Checkout Session

We have a basic scaffolding up and running, next let’s fetch a payment link that we can send to the end user.

The checkout functions requires 2 inputs:

  • Tier ID
  • User ID

Tier ID can be found if we call the /products endpoint, as shown below.

curl --header “apikey: ...“ http://127.0.0.1:3000/products

[ { “currency“: “usd“, “description“: “AJ Acct 2“, ... “tier_id“: “prod_M0ivDFxe9xTtvj“, ... “version_number“: 1 } ]

The User ID was added as part of creating the user in the previous sections, we can use the same user ajtest.

Build the checkout function using the values from above:

@app.route(“/checkout“)
def get_checkout():
    tier_id = “prod_M0ivDFxe9xTtvj“
    uid = “ajtest“
    return jsonify(archetype.create_checkout_session(uid=uid, tier_id=tier_id))

The final app.py would look something like this:

from flask import Flask, jsonify, request
from archetypewsgi import Archetype

app = Flask(name)

settings = {
    “app_id“: “8adb7f16fcef48668e4aaaab27f3179c“,
    “secret_key“: “archetype_sk_test_69ac9d65f206493c87faf190b35df056“,
}

archetype = Archetype(settings=settings)

app.wsgi_app = archetype.middleware(app=app.wsgi_app)

@app.route(“/checkout“)
def get_checkout():
    tier_id = “prod_M0ivDFxe9xTtvj“
    uid = “ajtest“
    return jsonify(archetype.create_checkout_session(uid=uid, tier_id=tier_id))

if name == “main“:
    app.run(port=3000)

If we curl the /checkout endpoint look for the url

curl --header “apikey: ...“ http://127.0.0.1:3000/checkout

{ ...

“url“: “https://checkout.stripe.com/pay/cs_test_a1aZtwLONzc3FsiQBcG2ctu1ZUD1NXhHjB9kKvnUgMtD98LhjXCRvtASjH#fidkdWxOYHwnPyd1blpxYHZxWjA0TjdtPVFOQERLXzBKSGR3XWRfaWNAf048b0dnYlFIak9ITkpIRF9BNUlyUW1nNzZORGRWUkp9XzV8T2dHN1Q2UlR8Vnd3STJrcnNidmo8XWE8ZlRUNDA1NTVVb219TTJ9ZycpJ2N3amhWYHdzYHcnP3F3cGApJ2lkfGpwcVF8dWAnPyd2bGtiaWBabHFgaCcpJ2BrZGdpYFVpZGZgbWppYWB3dic%2FcXdwYCkndnF3bHVgRGZmanBrcSc%2FJ2RmZnFaNElMbVU8Qmdxf2l3YTNGayd4JSUl" }

Send this url to the end user and they will be able to make a payment for the subscription using Stripe.

That’s it! Now we have our own app integrated with Archetype API. We can read further into the details of this integration and more on https://docs.archetype.dev.

Sign up to our newsletter

Get the latest articles on all things data delivered straight to your inbox.

Share:

Latest Articles

Don’t undersell your API!

Mon Sep 12 2022

Don’t undersell your API!

Getting customers is hard! From API-first startups to larger enterprise organizations, companies are launching and selling various API products to generate revenues for their businesses. The core of having a successful API comes in two parts. The first is the ability to build a product that people love and use often. The second is around efficiently pricing your product to ensure you are generating maximum value across your entire customer spectrum. We will explore the various ways someone can

The Basics of the Python SDK

Wed Aug 17 2022

The Basics of the Python SDK

In the last couple of blogs we discussed the different ways we can monetize our API. In this blog we’ll go over some of the implementation specifics and how we can integrate this with our app. In this post we’ll specifically focus on working with the Python SDK. Initializing SDK Now that we have some context, let’s go ahead and install the python SDK. We have the python package published to pypi.org, under the name archetypewsgi. pip install archetypewsgi Note: If we have python3 insta

Revenue Optimization for Volume Discount Plans: Case Study

Thu Aug 11 2022

Revenue Optimization for Volume Discount Plans: Case Study

In this article we will explore a sample case study as to how an API-first KYC company can adopt a usage based billing model to increase revenues and boost the customer experience for their users. As more companies are shifting from subscription models and annual contracts to usage based billing, teams are starting to analyze how they can apply this same switch to their businesses. We will outline a number of clear pain points mentioned by real customer conversations and how they are thinkin

API Monetization: For Revenue Teams

Fri Jul 15 2022

API Monetization: For Revenue Teams

API Monetization is the process of generating revenue from your APIs. The range of businesses that charge for their API products is quite large, from small seed stage companies to enterprises. The focus of this article will be around the various ways to think about API monetization.

What is Usage Based Billing?

Mon Jul 11 2022

What is Usage Based Billing?

There are many different ways to charge users for the same experience. In this blog post, we'll explore the different recurring revenue models you can have as a business and the tradeoffs for each business model.

Why API Marketplaces Suck

Sat Feb 26 2022

Why API Marketplaces Suck

API marketplaces have been on the rise as a way for companies to find and use APIs. However, there are many reasons why API marketplaces don’t work.

Ready to dive in?