Quickstart

This page gives you a brief introduction to the library. If you don’t have the library installed yet, check Installation first.

Minimal example

Let’s make our first API interaction. For this example we want to get a stock from the Stock Price API.

First, create a file called main.py and add following code (explanation below):

import asyncio
from apininjas import Client

async def main():
    async with Client("your_api_key") as client:
        stock = await client.fetch_stock("AAPL")
        print(f"{stock.name} is trading at ${stock.price}")

if __name__ == "__main__":
    asyncio.run(main())

It’s a really basic example, but let’s have a look what happens here:

  1. First lines imports asyncio and the Client to interact with the API.

  2. Inside the main function we create our Client with our API key as a context manager.

  3. After that, we retrieve the Stock with ticker AAPL via the fetch_stock() method.

  4. We then print out the name and price of the stock.

  5. Finally we execute our main coroutine with asyncio.run().


Now let’s run the script.

On Linux/MacOS, use

python3 main.py

On Windows, use

py -3 main.py

This should output something like this

Apple Inc. is trading at $220.11