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_iban_validation
- asyncfetch_inflation
- asyncfetch_inflations
- 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:
- async fetch_iban_validation(iban)¶
This function is a coroutine.
Retrieves an
IBANValidation.- Parameters:
iban (
str) – The IBAN to retrieve from.- Raises:
HTTPException – Retrieving the IBAN validation failed.
- Returns:
The retrieved IBAN validation.
- Return type:
- async fetch_inflation(country, *, type=...)¶
This function is a coroutine.
Retrieves an
Inflationwith the specified country.- Parameters:
country (
str) – The country to retrieve from.type (
InflationIndicatorType) – The inflation indicator type.
- Raises:
HTTPException – Retrieving the inflation failed.
- Returns:
The retrieved inflation.
- Return type:
- async fetch_inflations(*, type=...)¶
This function is a coroutine.
Retrieves a list of available
Inflation.- Parameters:
type (
InflationIndicatorType) – The inflation indicator type.- Raises:
HTTPException – Retrieving the inflation failed.
- Returns:
The retrieved list of available inflation.
- Return type:
List[
Inflation]
Utilities¶
- apininjas.utils.from_timestamp(timestamp, /)¶
A helper function that converts a given timestamp into a
datetimeobject.- Parameters:
timestamp (
int) – The timestamp to convert.- Returns:
The datetime object from the given timestamp.
- Return type:
- apininjas.utils.MISSING¶
A type-safe sentinel used within the library to indicate a missing value, distinct from
None.
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.
InflationIndicatorType¶
InflationCountry¶
- class apininjas.InflationCountry¶
Specifies the inflation country.
- austria¶
Austria.
- belgium¶
Belgium.
- brazil¶
Brazil.
- canada¶
Canada.
- chile¶
Chile.
- china¶
China.
- czech_republic¶
Czech Republic.
- czechia¶
Alias of
czech_republic.
- denmark¶
Denmark.
- estonia¶
Estonia.
- finland¶
Finland.
- france¶
France.
- germany¶
Germany.
- greece¶
Greece.
- hungary¶
Hungary.
- iceland¶
Iceland.
- india¶
India.
- indonesia¶
Indonesia.
- ireland¶
Ireland.
- israel¶
Israel.
- italy¶
Italy.
- japan¶
Japan.
- mexico¶
Mexico.
- norway¶
Norway.
- poland¶
Poland.
- portugal¶
Portugal.
- russia¶
Russia.
- slovakia¶
Slovakia.
- slovenia¶
Slovenia.
- south_korea¶
South Korea.
- south_africa¶
South Africa.
- spain¶
Spain.
- sweden¶
Sweden.
- switzerland¶
Switzerland.
- netherlands¶
The Netherlands.
- turkiye¶
Türkiye.
- united_kingdom¶
United Kingdom.
- uk¶
Alias of
united_kingdom.
- united_states¶
United States.
- usa¶
Alias of
united_states.
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.
- str(x)
Returns the stock’s name.
- 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.
- str(x)
Returns the name of the commodity future.
- 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.
- str(x)
Returns the cryptocurrency’s symbol.
- 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.
- str(x)
Returns the currency’s name.
- 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:
IBANValidation¶
- defis_valid
- class apininjas.IBANValidation¶
Represents an International Bank Account Number (IBAN) validation from the IBAN API.
- str(x)
Returns the IBAN.
- x == y
Checks if two IBANs are equal.
- x != y
Checks if two IBANs are not equal.
- valid¶
Whether the IBAN is valid or not.
Note
This only includes the IBAN checksum validation.
- Type:
Inflation¶
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.