Python SDK - Usage Guide

Important: Never expose your API key in client-side code or commit it to public repositories. Always use environment variables or a secure configuration management system.

Installation

You can install the Python SDK using pip:

pip install awesome-api-sdk

Initialization

Before you can make requests, you need to initialize the client with your API key.

Using an API Key directly


from awesome_api_sdk import AwesomeAPIClient

# Replace 'YOUR_API_KEY' with your actual API key
client = AwesomeAPIClient(api_key='YOUR_API_KEY')
        

Using an Environment Variable

It's recommended to use environment variables for storing your API key. Set the AWESOME_API_KEY environment variable before running your script.


import os
from awesome_api_sdk import AwesomeAPIClient

# The client will automatically look for the AWESOME_API_KEY environment variable
client = AwesomeAPIClient()

# If you want to explicitly specify the environment variable name:
# client = AwesomeAPIClient(api_key_env_var='MY_CUSTOM_API_KEY_VAR')
        

Making Your First Request

Let's make a simple request to get some data.

Fetching User Data

This example shows how to fetch details for a specific user.


try:
    user_id = "user_12345"
    user_data = client.users.get(user_id=user_id)
    print(f"Successfully fetched data for user {user_id}:")
    print(user_data)
except Exception as e:
    print(f"An error occurred: {e}")
        

Example Response

Example Output:

{
    "id": "user_12345",
    "username": "john_doe",
    "email": "john.doe@example.com",
    "created_at": "2023-10-27T10:30:00Z",
    "status": "active"
}
            

Common Operations

Listing Resources

To get a list of resources, use the list method. You can often apply filters and pagination.


try:
    # Fetch the first 10 active users
    users = client.users.list(status='active', limit=10)
    print("Listing active users:")
    for user in users:
        print(f"- {user['username']} ({user['id']})")
except Exception as e:
    print(f"An error occurred: {e}")
        

Creating Resources

Use the create method to add new resources.


try:
    new_user_data = {
        "username": "jane_doe",
        "email": "jane.doe@example.com",
        "password": "securepassword123" # Be mindful of password handling
    }
    created_user = client.users.create(**new_user_data)
    print(f"Successfully created user: {created_user['username']} (ID: {created_user['id']})")
except Exception as e:
    print(f"An error occurred: {e}")
        

Updating Resources

To update an existing resource, provide its ID and the fields you want to change.


try:
    user_id_to_update = "user_67890"
    update_payload = {
        "email": "jane.doe.updated@example.com",
        "status": "inactive"
    }
    updated_user = client.users.update(user_id=user_id_to_update, **update_payload)
    print(f"Successfully updated user {user_id_to_update}:")
    print(updated_user)
except Exception as e:
    print(f"An error occurred: {e}")
        

Deleting Resources

Use the delete method to remove a resource.


try:
    user_id_to_delete = "user_abcde"
    client.users.delete(user_id=user_id_to_delete)
    print(f"Successfully deleted user {user_id_to_delete}.")
except Exception as e:
    print(f"An error occurred: {e}")
        

Error Handling

The SDK raises exceptions for API errors. You should wrap your API calls in try-except blocks to handle potential issues gracefully.

Note: The SDK provides specific exception classes for different error types (e.g., AwesomeAPIError, NotFoundError, AuthenticationError) for more granular error handling. Check the API reference for a full list.

Rate Limiting

Our API has rate limits to ensure fair usage. The SDK may provide mechanisms to check current rate limit status or to handle rate limit errors (e.g., HTTP status 429). Consult the SDK's specific implementation for details on rate limiting.

Advanced Features

The SDK supports other features such as asynchronous operations, custom headers, and more. Explore the API Reference for a complete list of available methods and parameters.