# TypeScript

In TypeScript, we'll be using the `fetch` API to send HTTP requests. Here's how you can use the book search API with TypeScript:

First, define an interface for the book data:

```typescript
interface Book {
    title: string;
    author: string;
}

```

Next, write the `searchBook` function using async/await:

```typescript
async function searchBook(query: string): Promise<Book[]> {
    const url = 'https://api.promptjoy.com/api/mVMCpq';
    const options: RequestInit = {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'x-api-key': process.env.PROMPTJOY_API_KEY
        },
        body: JSON.stringify({ query })
    };

    const response = await fetch(url, options);

    if (!response.ok) {
        throw new Error(`HTTP error! status: ${response.status}`);
    }

    const data: Book[] = await response.json();
    return data;
}

```

You can call this function with the search term as follows:

```typescript
searchBook('your_search_term')
    .then(data => console.log(data))
    .catch(error => console.log('There was an error!', error));

```

This example uses the Fetch API, which returns Promises. If you're not familiar with Promises or async/await syntax, you might want to read up on those.

Remember to handle exceptions and errors as needed in your actual application code.

Note: In this example, the API key is retrieved from environment variables for security reasons. Ensure that you've set the `PROMPTJOY_API_KEY` environment variable in your environment where this code will be executed. If you're running this in a browser, you'll need to set this in some secure way, as exposing your API key in the client-side JavaScript code is not recommended.

\
\
\
\ <br>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.promptjoy.com/api/api-usage/typescript.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
