Add a book component
Save this as BookMarketBook.astro and pass real author and book slugs. It works in server-rendered pages and at build time in a static site.
---
interface Props { author: string; slug: string }
const { author, slug } = Astro.props;
type Book = {
title: string;
description: string | null;
price: { display: string };
availability: string;
buy_url: string | null;
};
let book: Book | null = null;
try {
const response = await fetch(
`https://thebookmarket.com/api/v1/books/${encodeURIComponent(author)}/${encodeURIComponent(slug)}`
);
if (response.ok) book = await response.json() as Book;
} catch { /* Render the unavailable message below. */ }
---
{book ? <article>
<h2>{book.title}</h2>
<p>{book.description}</p>
{book.availability === 'on_sale' && book.buy_url
? <a href={book.buy_url}>Buy ebook — {book.price.display}</a>
: <p>This book is currently unavailable.</p>}
</article> : <p>Book details are unavailable. Please try again later.</p>}
Use it in your page:
---
import BookMarketBook from '../components/BookMarketBook.astro';
---
<BookMarketBook author="YOUR-AUTHOR-SLUG" slug="YOUR-BOOK-SLUG" />
Keep the page current
On a static site the price and availability reflect your last build. Rebuild after changes, or use the browser fetch example for details fetched when the page opens. Checkout remains the place the reader sees the current purchase terms.
The author must activate selling and publish. The API needs no key and readers can buy without an account. Avoid set:html for catalog text. See the API reference for caching, rate limits and errors.