API Reference¶
This section provides a detailed overview of the API of apininjas.py.
Clients¶
Client¶
- asyncclose
- asyncfetch_commodity
- asyncfetch_crypto
- asyncfetch_crypto_symbols
- asyncfetch_currency
- asyncfetch_currency_conversion
- asyncfetch_stock
- defis_closed
- class apininjas.Client(api_key)¶
Represents a client that interacts with the API.
- async with x
Asynchronous context manager for the client that automatically cleans up.
- Parameters:
api_key (
str) – The API key to authenticate.
- async close()¶
Closes the client.
This closes all connections to the API.
- async fetch_stock(ticker)¶
This function is a coroutine.
Retrieves a
Stockwith the specified ticker.- Parameters:
ticker (
str) – The ticker to fetch from.- Raises:
StockNotFound – The stock with the specified ticker could not be found.
HTTPException – Retrieving the stock failed.
- Returns:
The retrieved stock.
- Return type:
- async fetch_commodity(type)¶
This function is a coroutine.
Retrieves a
Commoditywith the specified type.- Parameters:
type (
CommodityType) – The type of the commodity to fetch from.- Raises:
HTTPException – Retrieving the commodity failed.
- Returns:
The retrieved commodity.
- Return type:
- async fetch_crypto(symbol)¶
This function is a coroutine.
Retrieves a
Cryptowith the specified symbol.- Parameters:
symbol (
str) – The symbol to fetch from.- Raises:
HTTPException – Retrieving the cryptocurrency failed.
- Returns:
The retrieved cryptocurrency.
- Return type:
- async fetch_crypto_symbols()¶
This function is a coroutine.
Retrieves a list of all available cryptocurrency symbols.
- Raises:
HTTPException – Retrieving the symbols failed.
- Returns:
The retrieved list of available symbols.
- Return type:
List[
str]
- async fetch_currency_conversion(*, have, have_amount, want)¶
This function is a coroutine.
Retrieves a converted
CurrencyWithAmountfrom the specifiedhavecurrency andhave_amountand returns the old and new one.- Parameters:
- Raises:
HTTPException – Retrieving the currency failed.
- Returns:
The old and newly retrieved currency with their respective amounts.
- Return type:
Tuple[
CurrencyWithAmount,CurrencyWithAmount]
- async fetch_currency(name, *, reference)¶
This function is a coroutine.
Retrieves a
Currencywith the specified name and reference.- Parameters:
- Raises:
HTTPException – Retrieving the currency failed.
- Returns:
The retrieved currency.
- Return type:
Utility Functions¶
Enumerations¶
All enumerations inherit from enum.Enum.
CommodityType¶
- class apininjas.CommodityType¶
Specifies the type of a commodity future.
- gold¶
Gold Futures.
- soybean_oil¶
Soybean Oil Futures.
- wheat¶
Wheat Futures.
- platinum¶
Platinum Futures.
- micro_silver¶
Micro Silver Futures.
- lean_hogs¶
Lean Hogs Futures.
- corn¶
Corn Futures.
- oat¶
Oat Futures.
- aluminum¶
Aluminum Futures.
- soybean_meal¶
Soybean Meal Futures.
- silver¶
Silver Futures.
- soybean¶
Soybean Futures.
- lumber¶
Lumber Futures.
- live_cattle¶
Live Cattle Futures.
- sugar¶
Sugar Futures.
- natural_gas¶
Natural Gas Futures.
- crude_oil¶
Crude Oil Futures.
- orange_juice¶
Orange Juice Futures.
- coffee¶
Coffee Futures.
- cotton¶
Cotton Futures.
- copper¶
Copper Futures.
- micro_gold¶
Micro Gold Futures.
- feeder_cattle¶
Feeder Cattle Futures.
- rough_rice¶
Rough Rice Futures.
- palladium¶
Palladium Futures.
- cocoa¶
Cocoa Futures.
- brent_crude_oil¶
Brent Crude Oil Futures.
- gasoline_rbob¶
Gasoline RBOB Futures.
- heating_oil¶
Heating Oil Futures.
- class_3_milk¶
Class III Milk Futures.
Abstract Base Classes¶
FinancialInstrument¶
- asyncupdate
- class apininjas.abc.FinancialInstrument¶
An ABC representing the common operations of a financial instrument.
Following classes inherit from this ABC:
- price¶
The current price of the instrument, last updated at
updated_at.- Type:
- async update()¶
This function is a coroutine.
Updates
priceandupdated_atof the current object and returns the new price.Note
This makes an API call.
- Raises:
HTTPException – Retrieving the price failed.
- Returns:
The newly updated price.
- Return type:
Models¶
Stock¶
- class apininjas.Stock¶
Represents a stock from the Stock Price API.
- x == y
Checks if two stocks are equal.
- x != y
Checks if two stocks are not equal.
- x < y
Checks if a stock’s price is less than another.
- x > y
Checks if a stock’s price is greater than another.
- x <= y
Checks if a stock’s price is less or equal than another.
- x >= y
Checks if a stock’s price is greater or equal than another.
- exchange¶
The stock exchange the stock is traded on.
Noneif it’s not an exchange for stocks.- Type:
Optional[
str]
- price¶
The current price of the stock, last updated at
updated_at.- Type:
- async update()¶
This function is a coroutine.
Updates
priceandupdated_atof the current object and returns the new price.Note
This makes an API call.
- Raises:
HTTPException – Retrieving the price failed.
- Returns:
The newly updated price.
- Return type:
Commodity¶
- class apininjas.Commodity¶
Represents a commodity future from the Commodity Price API or the Gold Price API.
- x == y
Checks if two commodity futures are equal.
- x != y
Checks if two commodity futures are not equal.
- x < y
Checks if the price of a commodity future is less than the one of another.
- x > y
Checks if the price of a commodity future is greater than the one of another.
- x <= y
Checks if the price of a commodity future is less or equal than the one of another.
- x >= y
Checks if the price of a commodity future is greater or equal than the one of another.
- price¶
The current price of the commodity future, last updated at
updated_at.- Type:
- type¶
The type of the commodity future.
- Type:
- async update()¶
This function is a coroutine.
Updates
priceandupdated_atof the current object and returns the new price.Note
This makes an API call.
- Raises:
HTTPException – Retrieving the price failed.
- Returns:
The newly updated price.
- Return type:
Crypto¶
- asyncupdate
- class apininjas.Crypto¶
Represents a cryptocurrency from the Crypto Price API.
- x == y
Checks if two cryptocurrencies are equal.
- x != y
Checks if two cryptocurrencies are not equal.
- x < y
Checks if a cryptocurrency’s price is less than another.
- x > y
Checks if a cryptocurrency’s price is greater than another.
- x <= y
Checks if a cryptocurrency’s price is less or equal than another.
- x >= y
Checks if a cryptocurrency’s price is greater or equal than another.
- price¶
The current price of the cryptocurrency, last updated at
updated_at.- Type:
- async update()¶
This function is a coroutine.
Updates
priceandupdated_atof the current object and returns the new price.Note
This makes an API call.
- Raises:
HTTPException – Retrieving the price failed.
- Returns:
The newly updated price.
- Return type:
Currency¶
- defis_stronger
- asyncupdate
- class apininjas.Currency¶
Represents a currency from the Exchange Rate API or Currency Conversion API.
- x == y
Checks if two currencies are equal.
- x != y
Checks if two currencies are not equal.
- reference¶
The name of the reference currency.
- Type:
- class:
str
- async update()¶
This function is a coroutine.
Updates
exchange_rateof the current object and returns the new exchange rate.Note
This makes an API call.
- Raises:
HTTPException – Retrieving the exchange rate failed.
- Returns:
The newly updated exchange rate.
- Return type:
Exceptions¶
- exception apininjas.APINinjasBaseException¶
Base exception class.
Every exception in this library is derived from this class.
- exception apininjas.ClientException¶
Exception that’s raised when an operation in the
Clientfails.This exception is often raised due to invalid user input.
- exception apininjas.HTTPException(response, data)¶
HTTP Exception that’s raised when an HTTP request fails.
- response¶
The response of the HTTP request.
- Type:
- status¶
The HTTP status code.
- Type:
- exception apininjas.NotFound(response, data)¶
HTTP Exception with status code 404.
Derives from
HTTPException.
- exception apininjas.MethodNotAllowed(response, data)¶
HTTP Exception with status code 405.
Derives from
HTTPException.
- exception apininjas.APINinjasServerError(response, data)¶
HTTP Exception with status code above 500.
Derives from
HTTPException.
- exception apininjas.StockNotFound¶
Exception that’s raised when a requested stock could not be found.
This exception is raised when the data returned by the API is invalid due to invalid user input.
Derives from
ClientException.