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:
First lines imports
asyncioand theClientto interact with the API.Inside the
mainfunction we create ourClientwith our API key as a context manager.After that, we retrieve the
Stockwith tickerAAPLvia thefetch_stock()method.Finally we execute our
maincoroutine withasyncio.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