Building A Book Search Engine with PromptJoy and Next.js
In this tutorial, we are going to build a book search engine using Next.js, an open-source development framework built on top of Node.js. We'll be leveraging two APIs provided by PromptJoy: the Book Search API and the Book Summary API. These APIs will allow our search engine to retrieve book results based on user queries and provide a detailed summary for each book.
One of the exciting features of PromptJoy is that you can create your own APIs to customize the functionality of your search engine. For instance, you could create APIs for searching and summarizing different types of content like movies, songs, recipes, etc.
PromptJoy APIs
Book Search API: This API (https://preview.promptjoy.com/apis/mVMCpq) acts as a book search engine. When provided with a query, it returns 3 book results that match the query. Each result includes the title and author of a book.
Endpoint:
https://api.promptjoy.com/api/mVMCpq
Method:
POST
API Key: You need to include your unique API key in the request header to authenticate your requests. We'll discuss how to set this up later in the tutorial.
Book Summary API: This API (https://preview.promptjoy.com/apis/jJbC2p) acts as a book database. When given the title and author of a book, it returns a detailed summary of the book.
Endpoint:
https://api.promptjoy.com/api/jJbC2p
Method:
POST
API Key: You need to include your unique API key in the request header to authenticate your requests.
Project Setup
First, let's create a new Next.js application. We are using Next.js version 12 in this tutorial. You can do this by running the following command in your terminal:
Then, navigate into your new project directory:
Next, install the necessary dependencies. We will be using axios for making HTTP requests and react-spinners for displaying a loading spinner:
Creating the Search Engine
Now, let's create our book search engine. We'll need to create a form for users to enter their search queries, a component to display the search results, and a function to handle the search.
In your pages
directory, create a new file called index.js
and copy the following code:
In this code, we have a Home
component which acts as the main page of our application. Inside this component, we have a form that takes in a search query from the user. When the form is submitted, it triggers the searchBooks
function which makes a request to the Book Search API with the provided query, retrieves the results, and sets them in the books
state.
The books
state is then mapped to our Book
component which displays each book title and author. The title of the book is clickable, and when clicked, it triggers the fetchSummary
function. This function makes a request to the Book Summary API with the book title and author, retrieves the book summary, and displays it in an alert.
Finally, we have a loading spinner that displays while the requests to the APIs are being processed. The spinner is centered under the search button and will disappear once the requests are complete.
Deploying to Vercel
Deploying your Next.js application to Vercel is straightforward. First, you need to push your application to a GitHub repository. Then, go to Vercel, sign up for a new account or log in to your existing one, and import your GitHub repository. Vercel will automatically detect that your application is a Next.js app and will provide sensible default configurations.
Lastly, you'll need to set your PromptJoy API key as an environment variable in Vercel. You can do this by going to your project settings, thento the "Environment Variables" section, and adding a new variable with the name PROMPTJOY_API_KEY
and the value being your actual API key.
Once that's done, click "Deploy", and Vercel will automatically build and deploy your application.
And there you have it! You've built a book search engine using Next.js and PromptJoy. This is just the beginning, though. The great thing about PromptJoy is that you can build your own APIs to create a search engine that's perfectly tailored to your needs. Whether you want to search for books, movies, music, or anything else, you can create an API for it and integrate it into your search engine.
Remember to secure your API keys and to regularly update your application to ensure its security and performance. Happy coding!
Last updated