API Usage

Using an API created with PromptJoy can be done in several programming languages. This guide will show you how to use the book search API (https://preview.promptjoy.com/apis/mVMCpq) we created earlier. For this API, we will be making a POST request to https://api.promptjoy.com/api/mVMCpq with a JSON payload containing the book search query.

The general API request looks like this:

Endpoint: https://api.promptjoy.com/api/mVMCpq

HTTP Method: POST

Headers:

  • Content-Type: application/json

  • x-api-key: sk-6200906b6fcc732e9265028f53d5d6f01575a162

Data/Payload:

{
  "query": "your_search_term"
}

Before diving into language-specific examples, let's talk about how to store your API key as an environment variable. Environment variables are a great way to keep sensitive information like API keys out of your source code. Here's how to do it:

  1. Windows: Open a new Command Prompt window and type setx PROMPTJOY_API_KEY "your_api_key" then press Enter. Replace "your_api_key" with your actual API key.

  2. macOS/Linux: Open a new Terminal window and type export PROMPTJOY_API_KEY="your_api_key" then press Enter. Replace "your_api_key" with your actual API key.

You can then access this environment variable in your code using the specific syntax for your programming language. This will be covered in each language-specific example.

Here's how you can make the request using curl, a command-line tool used for transferring data with URLs.

curl -X POST https://api.promptjoy.com/api/mVMCpq \
-H 'Content-Type: application/json' \
-H 'x-api-key: '$PROMPTJOY_API_KEY \
-d '{"query":"your_search_term"}'

Replace "your_search_term" with the book you're searching for. The response will be a JSON object containing the search results.

For language-specific examples, please navigate to the sections for Python, JavaScript, Ruby, Go, Java, and C#. Each section will provide a detailed guide on how to use the book search API in the respective language.

Last updated