Skip to content

Python Examples

Here are practical examples of how to use the Regular Show API in Python.

Basic Setup

python
import requests

API_BASE = 'https://regularshow-api.vercel.app/api'

def fetch_api(endpoint):
    response = requests.get(f"{API_BASE}{endpoint}")
    response.raise_for_status()
    return response.json()

Get All Characters

python
def get_all_characters():
    data = fetch_api('/character')
    print(f"Found {data['info']['count']} characters")
    return data['results']

Search Characters

python
import urllib.parse

def search_characters(query):
    encoded = urllib.parse.quote(query)
    data = fetch_api(f'/character?name={encoded}')
    return data['results']

# Usage
results = search_characters('Mordecai')
for char in results:
    print(char['name'])

Get Character by ID

python
def get_character(id_or_slug):
    data = fetch_api(f'/character/{id_or_slug}')
    if 'error' in data:
        print(f"Error: {data['error']}")
        return None
    return data

# Usage
char = get_character('Benson')
if char:
    print(f"{char['name']} is voiced by {char['info']['Voiced_by']}")

Filter by Category

python
def get_villains():
    data = fetch_api('/character?category=Villains&limit=50')
    return data['results']

Get Episodes by Season

python
def get_season_episodes(season):
    data = fetch_api(f'/episode?season={season}')
    return data['results']

# Usage
episodes = get_season_episodes(1)
for ep in episodes:
    print(f"{ep['episode_number']}. {ep['name']}")

Paginated Requests

python
def get_all_paginated(endpoint, limit=20):
    all_results = []
    page = 1
    
    while True:
        data = fetch_api(f'{endpoint}?page={page}&limit={limit}')
        all_results.extend(data['results'])
        
        if page >= data['info']['pages']:
            break
        page += 1
    
    return all_results

# Usage
all_characters = get_all_paginated('/character', 50)
print(f"Fetched {len(all_characters)} characters")

Using with Async/Await

python
import aiohttp
import asyncio

async def fetch_async(endpoint):
    async with aiohttp.ClientSession() as session:
        async with session.get(f'{API_BASE}{endpoint}') as response:
            return await response.json()

async def main():
    data = await fetch_async('/character?limit=5')
    for char in data['results']:
        print(char['name'])

asyncio.run(main())

Flask Example

python
from flask import Flask, jsonify
import requests

app = Flask(__name__)

API_BASE = 'https://regularshow-api.vercel.app/api'

@app.route('/api/characters')
def characters():
    data = requests.get(f'{API_BASE}/character').json()
    return jsonify(data)

@app.route('/api/character/<path:name>')
def character(name):
    data = requests.get(f'{API_BASE}/character/{name}').json()
    return jsonify(data)

if __name__ == '__main__':
    app.run(debug=True)

Error Handling

python
def safe_fetch(endpoint):
    try:
        response = requests.get(f"{API_BASE}{endpoint}")
        response.raise_for_status()
        data = response.json()
        
        if 'error' in data:
            print(f"API Error: {data['error']}")
            return None
        
        return data
    except requests.exceptions.RequestException as e:
        print(f"Request Error: {e}")
        return None

Released under the MIT License.