openlibrary

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 16, 2026 License: GPL-3.0 Imports: 6 Imported by: 0

README

go-openlibrary

A Go client library for the Open Library API. This library provides a simple interface to search for books and retrieve detailed book information.

Features

  • Search for books by title, author, or any query string
  • Get detailed book information by ISBN (ISBN-10 or ISBN-13)
  • Generate cover image URLs in multiple sizes
  • Zero external dependencies (uses only Go standard library)
  • Customizable User-Agent for API requests

Installation

go get git.devvul.com/asara/go-openlibrary

Usage

Initialize the Client
import "git.devvul.com/asara/go-openlibrary"

// Initialize with your application details
client := openlibrary.Init("MyApp", "1.0", "[email protected]")

The Init() function requires three parameters:

  • name - Your application name
  • version - Your application version
  • email - Your contact email address

These are formatted into a User-Agent header as: {name}/{version} ({email})

These are required by openlibrary.org

Get Book by ISBN
book, err := client.BookByISBN("9780140328721")
if err != nil {
    log.Fatal(err)
}

fmt.Printf("Title: %s\n", book.Title)
fmt.Printf("Authors: %s\n", book.Authors)
fmt.Printf("Published: %s\n", book.PublishDate)
fmt.Printf("Pages: %d\n", book.Pages)

ISBNs can include hyphens and spaces - they will be automatically cleaned.

Search for Books
// Search with a limit of 10 results
results, err := client.SearchBooks("The Lord of the Rings", 10)
if err != nil {
    log.Fatal(err)
}

fmt.Printf("Found %d results\n", results.NumFound)
for _, doc := range results.Docs {
    fmt.Printf("%s by %v\n", doc.Title, doc.AuthorName)
}
Get Cover Image URL
// Get cover URL (this is a standalone function, doesn't require a client)
coverURL := openlibrary.GetCoverURL("9780140328721", "L")
fmt.Println(coverURL)
// Output: https://covers.openlibrary.org/b/isbn/9780140328721-L.jpg

Cover sizes:

  • "S" - Small
  • "M" - Medium
  • "L" - Large

API Reference

Types
BookResult

Simplified book information returned by BookByISBN():

type BookResult struct {
    Title       string
    Subtitle    string
    Authors     string   // Comma-separated author names
    Publishers  string   // Comma-separated publishers
    PublishDate string
    Pages       int
    ISBN10      string
    ISBN13      string
    CoverSmall  string
    CoverMedium string
    CoverLarge  string
    Subjects    string   // Comma-separated subjects
    Description string
    OLID        string   // Open Library ID
}
SearchResponse

Search results returned by SearchBooks():

type SearchResponse struct {
    NumFound int
    Docs     []SearchDoc
}

type SearchDoc struct {
    Title            string
    AuthorName       []string
    FirstPublishYear int
    ISBN             []string
    CoverI           int
    EditionCount     int
    Key              string
}
Methods
Init(name, version, email string) *OpenLibraryApi

Creates and returns a new API client instance.

BookByISBN(isbn string) (*BookResult, error)

Fetches detailed book information for the given ISBN-10 or ISBN-13.

SearchBooks(query string, limit int) (*SearchResponse, error)

Searches for books matching the query string. If limit is 0, defaults to 10 results.

GetCoverURL(isbn string, size string) string

Generates a cover image URL for the given ISBN and size. Size must be "S", "M", or "L" (defaults to "M" if invalid).

Complete Example

package main

import (
    "fmt"
    "log"

    "git.devvul.com/asara/go-openlibrary"
)

func main() {
    // Initialize the client
    client := openlibrary.Init("BookFinder", "1.0", "[email protected]")

    // Search for books
    results, err := client.SearchBooks("1984 George Orwell", 5)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("Found %d results\n\n", results.NumFound)

    // Get details for the first result if it has an ISBN
    if len(results.Docs) > 0 && len(results.Docs[0].ISBN) > 0 {
        isbn := results.Docs[0].ISBN[0]
        book, err := client.BookByISBN(isbn)
        if err != nil {
            log.Fatal(err)
        }

        fmt.Printf("Title: %s\n", book.Title)
        fmt.Printf("Authors: %s\n", book.Authors)
        fmt.Printf("Published: %s\n", book.PublishDate)
        fmt.Printf("Pages: %d\n", book.Pages)
        fmt.Printf("ISBN-13: %s\n", book.ISBN13)

        // Get cover image URL
        coverURL := openlibrary.GetCoverURL(isbn, "L")
        fmt.Printf("Cover: %s\n", coverURL)
    }
}

API Endpoints Used

This library interacts with the following Open Library API endpoints:

  • Books API: https://openlibrary.org/api/books?bibkeys=ISBN:{isbn}&jscmd=data&format=json
  • Search API: https://openlibrary.org/search.json?q={query}&limit={limit}
  • Covers API: https://covers.openlibrary.org/b/isbn/{isbn}-{size}.jpg

License

This library is provided as-is for interacting with the Open Library API. Please review Open Library's terms of use when using this library.

Contributing

Issues and pull requests are welcome.

Documentation

Overview

Package openlibrary is a golang implementation of the Open Library API.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetCoverURL

func GetCoverURL(isbn string, size string) string

GetCoverURL constructs a cover image URL given an ISBN and size size can be "S" (small), "M" (medium), or "L" (large)

func GetCoverURLByID added in v0.2.0

func GetCoverURLByID(coverID int, size string) string

GetCoverURLByID constructs a cover image URL given a cover ID and size The Works API returns cover IDs (ints), not URLs size can be "S" (small), "M" (medium), or "L" (large)

Types

type Author

type Author struct {
	Name string `json:"name"`
	URL  string `json:"url"`
}

Author represents an author in the Open Library API

type BookData

type BookData struct {
	Title         string      `json:"title"`
	Subtitle      string      `json:"subtitle"`
	Authors       []Author    `json:"authors"`
	Publishers    []Publisher `json:"publishers"`
	PublishDate   string      `json:"publish_date"`
	NumberOfPages int         `json:"number_of_pages"`
	Cover         Cover       `json:"cover"`
	Identifiers   Identifier  `json:"identifiers"`
	Subjects      []Subject   `json:"subjects"`
	Notes         string      `json:"notes"`
	URL           string      `json:"url"`
	Key           string      `json:"key"`
}

BookData is the detailed book data from the Books API

type BookResult

type BookResult struct {
	Title       string
	Subtitle    string
	Authors     string // Comma-separated author names
	Publishers  string // Comma-separated publishers
	PublishDate string
	Pages       int
	ISBN10      string
	ISBN13      string
	CoverSmall  string
	CoverMedium string
	CoverLarge  string
	Subjects    string // Comma-separated subjects
	Description string
	OLID        string // Open Library ID
	WorkKey     string // Open Library work key (e.g., "/works/OL81634W")
	Error       string
}

BookResult is the simplified result struct for our application

func (BookResult) String

func (br BookResult) String() string

Stringer Interface for BookResult

type BooksAPIResponse

type BooksAPIResponse struct {
	Data map[string]BookData
}

BooksAPIResponse is the wrapper response from the Books API

type Cover

type Cover struct {
	Small  string `json:"small"`
	Medium string `json:"medium"`
	Large  string `json:"large"`
}

Cover represents cover image URLs

type Identifier

type Identifier struct {
	ISBN10 []string `json:"isbn_10"`
	ISBN13 []string `json:"isbn_13"`
}

Identifier represents ISBN identifiers

type OpenLibraryApi

type OpenLibraryApi struct {
	// contains filtered or unexported fields
}

func Init

func Init(name, version, email string) *OpenLibraryApi

func (*OpenLibraryApi) BookByISBN

func (api *OpenLibraryApi) BookByISBN(isbn string) (*BookResult, error)

BookByISBN returns detailed book data given an ISBN (10 or 13 digit)

func (*OpenLibraryApi) SearchBooks

func (api *OpenLibraryApi) SearchBooks(query string, limit int) (*SearchResponse, error)

SearchBooks searches for books by query string

func (*OpenLibraryApi) WorkByKey added in v0.2.0

func (api *OpenLibraryApi) WorkByKey(key string) (*WorkResult, error)

WorkByKey fetches work-level data from the Open Library Works API

type Publisher

type Publisher struct {
	Name string `json:"name"`
}

Publisher represents a publisher

type SearchDoc

type SearchDoc struct {
	Title            string   `json:"title"`
	AuthorName       []string `json:"author_name"`
	FirstPublishYear int      `json:"first_publish_year"`
	ISBN             []string `json:"isbn"`
	CoverI           int      `json:"cover_i"`
	EditionCount     int      `json:"edition_count"`
	Key              string   `json:"key"`
}

SearchDoc represents a single search result document

type SearchResponse

type SearchResponse struct {
	NumFound int         `json:"numFound"`
	Docs     []SearchDoc `json:"docs"`
}

SearchResponse is the response from the search API

type Subject

type Subject struct {
	Name string `json:"name"`
	URL  string `json:"url"`
}

Subject represents a subject/genre

type WorkResult added in v0.2.0

type WorkResult struct {
	Title            string
	Description      string
	Subjects         string // Comma-separated subjects
	Covers           []int
	FirstPublishDate string
	Key              string
}

WorkResult represents a work-level result from the Open Library Works API

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL