Getting Started

**SaaSpec is currently compatible with only OpenAI models

SaaSpec is a powerful API analytics and monitoring tool designed to help developers manage and optimize their usage of pay-as-you-go APIs. By integrating SaaSpec into your application, you can track key metrics, detect anomalies, and implement dynamic billing models based on actual usage. SaaSpec is currently able to be utilized with the OpenAI models only.

Making an Account

Watch the following video to learn how to use the dashboard:

Quick Tutorial

Parameters for Making a Request

  • Who: (String) UID - A unique identifier for the user making the request. This helps in tracking individual usage and behavior. This needs to either match with how they are identified in Stripe or you need a way to match this identifier back to the correct user when creating the invoice.
  • input: (String) - The query that was sent to the LLM model API.
  • output: (String) - The response returned from the LLM model API.
  • what_api: (String) - The LLM model that you used. It is extremely important that this is from the response of the LLM request and not hard coded, as models are updated constantly by OpenAI, Google, etc. Making a more dynamic approach is needed.
  • api_token: (String) - This can be found in your SaaSpec dashboard in the profile section.

Additional Examples

Generic Request Example


import requests

api_url = 'https://us-central1-saaspect-424004.cloudfunctions.net/api_call/call'
# Define the payload with 5 variables
payload = {
    "Who": "*insert UID here",
    "api_token": "your SaaSpec project API token here",
    "input": "question",
    "output": "response from model here",
    "what_api": "gpt-4..etc",
}

# Send the POST request to the API
response = requests.post(api_url, json=payload)

          

**SaaSpec is currently compatible with only OpenAI models

OpenAI API Requests Example


import requests
from openai import OpenAI

# Define SaaSpec API endpoint
api_url = 'https://us-central1-saaspect-424004.cloudfunctions.net/api_call/call'
client = OpenAI(api_key="your-openai-api-key")

question = "Rephrase this text..."
completion = client.chat.completions.create(
  model="gpt-4",
  messages=[
    {"role": "system", "content": "You are a school teacher, skilled in explaining concepts and editing school work."},
    {"role": "user", "content": question}
  ]
)

# Define the payload with 5 variables
payload = {
    "Who": "*insert UID here",
    "api_token": "your SaaSpec project API token here",
    "input": question,
    "output": completion.choices[0].message.content,
    "what_api": completion.model,
}

# Send the POST request to the API
response = requests.post(api_url, json=payload)

# Check the response
if response.status_code == 200:
    print("Success:", response.json())
else:
    print("Failed:", response.status_code, response.text)

          

Example SaaSpec to Stripe Invoice code


  import csv
  import stripe
  
  # Stripe secret key
  stripe.api_key = 'your_secret_key_here'
  
  def read_csv(file_path):
      users = []
      with open(file_path, mode='r', newline='') as file:
          csv_reader = csv.DictReader(file)
          for row in csv_reader:
              users.append({
                  'user': row['USER'],
                  'token_cost': float(row['TOKEN COST'].replace('$', '').strip()),
                  'input_count': int(row['INPUT COUNT']),
                  'output_count': int(row['OUTPUT COUNT']),
                  'anomaly': row['ANOMALY']
              })
      return users
  
  def create_invoice(user, token_cost):
      # Find or create a customer
      customers = stripe.Customer.list(email=user)
      if customers['data']:
          customer_id = customers['data'][0]['id']
      else:
          customer = stripe.Customer.create(email=user)
          customer_id = customer.id
      
      # Create an invoice item
      stripe.InvoiceItem.create(
          customer=customer_id,
          amount=int(token_cost * 100),  # Stripe amount is in cents
          currency='usd',
          description=f'Token usage for {user}'
      )
  
      # Create and send the invoice
      invoice = stripe.Invoice.create(
          customer=customer_id,
          auto_advance=True  # Auto-finalize this draft after ~1 hour
      )
      invoice.send_invoice()
  
  # Read CSV and create invoices
  csv_file_path = '/mnt/data/users_data.csv'  # replace with your actual CSV file path
  users_data = read_csv(csv_file_path)
  for user_data in users_data:
      create_invoice(user_data['user'], user_data['token_cost'])