Stonks API is a serverless API developed to fetch stock data from multiple providers using Cheerio, with the initial implementation focusing on Finvinz. Hosted on Vercel, this API aims to extend support to various financial data providers, making it a versatile tool for accessing stock market data.
- Fetch stock data including price, price changes, and percentage changes.
- Currently supports Finvinz.
- Designed to easily extend support to additional providers.
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
What things you need to install the software and how to install them:
Node version: 18.x
npm install -g vercel
A step by step series of examples that tell you how to get a development env running:
-
Clone the repository:
git clone https://github.com/matextrem/stonks-api.git cd stonks-api
-
Install dependencies:
yarn install
-
Set up environment variables: Create a
.env
file in the root directory and add the following:API_PROVIDER=finvinz
-
Run the development server:
npm run dev
To fetch stock data from the supported providers, navigate to:
http://localhost:3000/api/quote/AAPL // Default to type=stock
Replace AAPL
with your desired stock ticker.
We also support the following types:
- FOREX
http://localhost:3000/api/quote/EURUSD?type=forex
- COMMODITIES
http://localhost:3000/api/quote/GOLD?type=commodity
- FUTURES
http://localhost:3000/api/quote/NQ?type=future
- CRYPTO
http://localhost:3000/api/quote/ethereum?type=crypto
The API is designed to be extendable with multiple providers. It is configured as follows:
export const PROVIDER: ApiProviders =
(process.env.API_PROVIDER as ApiProviders) || ApiProviders.Finviz;
export const API_PROVIDERS: ApiProvidersConfig = {
[ApiProviders.Finviz]: {
baseUrl: 'https://finviz.com',
endpoints: {
stock: {
route: 'quote.ashx',
query: 't',
},
forex: {
route: 'currencies',
fallback: ApiProviders.Investing,
},
commodity: {
route: 'commodities',
fallback: ApiProviders.Investing,
},
future: {
route: 'indices',
fallback: ApiProviders.Investing,
},
crypto: {
route: 'crypto',
fallback: ApiProviders.CoinMarketCap,
},
},
selectors: {
name: {
selector: '.quote-header_ticker-wrapper_company > a',
extractor: (element: cheerio.Cheerio<cheerio.Element>) =>
element.text().trim(),
},
ticker: {
selector: '.quote-header_ticker-wrapper_ticker',
extractor: (element: cheerio.Cheerio<cheerio.Element>) =>
element.text().trim(),
},
price: {
selector: '.quote-price_wrapper_price',
extractor: (element: cheerio.Cheerio<cheerio.Element>) =>
element.text().trim(),
},
change: {
selector: '.quote-price_wrapper_change > tbody > tr',
extractor: (
element: cheerio.Cheerio<cheerio.Element>,
$: cheerio.CheerioAPI
) => {
let value = '';
element.find('.sr-only').each((i: number, el: cheerio.Element) => {
if ($(el).text().includes('Dollar')) {
value = $(el).parent().text().replace($(el).text(), '').trim();
}
});
return value;
},
},
percentageChange: {
selector: '.quote-price_wrapper_change > tbody > tr',
extractor: (
element: cheerio.Cheerio<cheerio.Element>,
$: cheerio.CheerioAPI
) => {
let value = '';
element.find('.sr-only').each((i: number, el: cheerio.Element) => {
if ($(el).text().includes('Percentage')) {
value = $(el).parent().text().replace($(el).text(), '').trim();
}
});
return value;
},
},
},
}
};
Contributions are what make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
This project is licensed under the ISC License - see the LICENSE file for details.