The Book Market

Next.js and React

Fetch a public book in a React component and link to its checkout URL.

Updated

Read as Markdown
Show the prompt for your AI

Use The Book Market (thebookmarket.com) to sell my ebook on my website. Read https://thebookmarket.com/llms.txt first, then follow its instructions.

Add a book component

Save this as BookMarketBook.tsx. It is a client component for a Next.js app and can also run in a React app. Replace the slug props where you use it. No API key is required.

'use client';

import { useEffect, useState } from 'react';

type Book = {
  title: string;
  description: string | null;
  price: { display: string };
  availability: 'on_sale' | 'unavailable' | 'coming_soon';
  buy_url: string | null;
};

export default function BookMarketBook({ author, slug }: { author: string; slug: string }) {
  const [result, setResult] = useState<{ key: string; book: Book | null } | null>(null);
  const key = `${author}/${slug}`;
  useEffect(() => {
    const controller = new AbortController();
    fetch(`https://thebookmarket.com/api/v1/books/${encodeURIComponent(author)}/${encodeURIComponent(slug)}`, {
      signal: controller.signal,
    })
      .then(response => {
        if (!response.ok) throw new Error('Book unavailable');
        return response.json() as Promise<Book>;
      })
      .then(book => setResult({ key, book }))
      .catch(() => {
        if (!controller.signal.aborted) setResult({ key, book: null });
      });
    return () => controller.abort();
  }, [author, slug, key]);

  if (!result || result.key !== key) return <p role="status">Loading book…</p>;
  const book = result.book;
  if (!book) return <p>Book details are unavailable. Please try again later.</p>;
  return <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>;
}
import BookMarketBook from './BookMarketBook';

export default function Page() {
  return <BookMarketBook author="YOUR-AUTHOR-SLUG" slug="YOUR-BOOK-SLUG" />;
}

Selling and availability

The author sets up and activates selling before the button can work. Readers can buy as guests. Render text through JSX; do not use dangerouslySetInnerHTML for book data.

coming_soon is reserved in the catalog schema and is not emitted today. Enable the button only for on_sale with a non-null buy_url. See the API reference for null fields, caching and errors. The embed is coming soon.