FreeApiHub SDK Guide
Welcome to the FreeApiHub SDK! This SDK provides a simple and effective way to access various free APIs for data retrieval and integration into your JavaScript or TypeScript projects.
Installation
To get started, install the FreeApiHub SDK via npm or pnpm:
Using npm
npm install freeapihub
Using pnpm
pnpm add freeapihub
ENV Configuration
To ensure the SDK connects to the correct API endpoint, you need to configure an environment variable in your project.
-
Create a .env file at the root of your project (if it doesn’t already exist).
-
Add the following line to specify the base URL for API requests:
API_BASE_URL="https://freeapihub.onrender.com"
This URL is required for the SDK functions to properly retrieve data from FreeApiHub.
Basic Usage
The FreeApiHub SDK provides functions for each available API, making it easy to fetch data with just a few lines of code. Below are some examples of how to use these APIs.
Example 1: Fetching All Books
import { getAllBooks } from "freeapihub";
async function fetchBooks() {
const books = await getAllBooks();
console.log(books);
}
fetchBooks();
Example 2: Fetching a Book by ID
import { getBookById } from "freeapihub";
async function fetchBookById() {
const book = await getBookById("your-book-id");
console.log(book);
}
fetchBookById();
Error Handling
Each API function is designed to handle errors gracefully. If an error occurs (e.g., network issues, invalid ID), it will throw an error that you can catch in your code:
import { getAllJokes } from "freeapihub";
async function fetchJokes() {
try {
const jokes = await getAllJokes();
console.log(jokes);
} catch (error) {
console.error("Failed to fetch jokes:", error);
}
}
fetchJokes();
Next Steps
Now that you have the FreeApiHub SDK set up and working, you can start integrating it into your project! You can explore other available APIs, like User, Joke, and Stock, using similar functions to fetch and display the data you need.
Happy coding, and enjoy the power of FreeApiHub!