Skip to content

Tutorial

API Key

Before anything, you'd like to get a API key from the website.

Note

You might want to keep the API key as an environment variable instead of pasting it directly into source code in most cases. To do this, install the python-dotenv library and create a .env file in the same directory as your code file.

.env

API_KEY="abcd1234"
main.py
import os

import dotenv

dotenv.load_dotenv()

KEY = os.environ["API_KEY"]

Installing the library

pip install marvelrivalsapi
poetry add marvelrivalsapi
uv add marvelrivalsapi

Use pip if you don't know what you're doing.

First Steps

The client objects are the entry point to the API and you'll mostly be using them to perform all the package related tasks.

import marvelrivalsapi

client = marvelrivalsapi.MarvelRivalsClient("mykeyhere")
# rest of code
import asyncio

import marvelrivalsapi

def main():
    client = marvelrivalsapi.AsyncMarvelRivalsClient("mykeyhere")
    # rest of code
    await client.close()

asyncio.run(main())
import asyncio

import marvelrivalsapi

def main():
    async with marvelrivalsapi.AsyncMarvelRivalsClient("mykeyhere") as client:
        # other stuff here
        ...

asyncio.run(main())

Example: Fetching a hero data

import marvelrivalsapi

client = marvelrivalsapi.MarvelRivalsClient("mykeyhere")
spiderman = client.get_hero(marvelrivalsapi.Heroes.SPIDER_MAN)
print(spiderman)
import asyncio

import marvelrivalsapi

def main():
    client = marvelrivalsapi.AsyncMarvelRivalsClient("mykeyhere")
    spiderman = await client.get_hero(marvelrivalsapi.Heroes.SPIDER_MAN)
    print(spiderman)
    await client.close()

asyncio.run(main())