webapi

package
v0.1.0 Latest Latest
Warning

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

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

Documentation

Overview

Package webapi is a small Spotify Web API client for search and saved albums. It logs in with the user's own developer app through the Authorization Code flow with PKCE, so it needs no client secret.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotConfigured means no client ID is set.
	ErrNotConfigured = errors.New("no Spotify developer app client ID is set")
	// ErrNotLoggedIn means there is no valid Web API token.
	ErrNotLoggedIn = errors.New("not logged in to the Spotify Web API")
)

Functions

func ArtistNames

func ArtistNames(artists []Artist) string

ArtistNames joins artist names with commas.

func PickImage

func PickImage(images []Image) string

PickImage returns the URL of the smallest image that is at least 300 pixels wide, or of the first image if no width is known.

Types

type APIError

type APIError struct {
	Status  int
	Message string
}

APIError is an error reply from the Web API.

func (*APIError) Error

func (e *APIError) Error() string

type Album

type Album struct {
	URI         string   `json:"uri"`
	Name        string   `json:"name"`
	Artists     []Artist `json:"artists"`
	ReleaseDate string   `json:"release_date"`
	TotalTracks int      `json:"total_tracks"`
	Images      []Image  `json:"images"`
}

Album is a Spotify album.

func (Album) Year

func (a Album) Year() string

Year returns the release year, or an empty string.

type Artist

type Artist struct {
	URI    string  `json:"uri"`
	Name   string  `json:"name"`
	Images []Image `json:"images"`
}

Artist is a Spotify artist. Artists inside track and album objects have no images.

type Client

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

Client calls the Spotify Web API.

func New

func New(clientID string, port int, tokenPath string) *Client

New creates a client. It loads a saved token from tokenPath if one exists.

func (*Client) BeginLogin

func (c *Client) BeginLogin() (*Login, error)

BeginLogin starts the callback server and returns the URL the user must open in a browser.

func (*Client) Configured

func (c *Client) Configured() bool

Configured reports whether a client ID is set.

func (*Client) LoggedIn

func (c *Client) LoggedIn() bool

LoggedIn reports whether the client has a token.

func (*Client) Logout

func (c *Client) Logout() error

Logout forgets the token and deletes the token file.

func (*Client) SavedAlbums

func (c *Client) SavedAlbums(ctx context.Context) ([]Album, error)

SavedAlbums returns the albums in the user's library, newest first.

func (*Client) Search

func (c *Client) Search(ctx context.Context, query string) (*SearchResult, error)

Search finds tracks, albums, artists, and playlists. Development-mode apps get at most 10 results per type.

type Image

type Image struct {
	URL   string `json:"url"`
	Width *int   `json:"width"`
}

Image is one size of a cover or artist picture.

type Login

type Login struct {
	URL string
	// contains filtered or unexported fields
}

Login is a login in progress. Show URL to the user, then call Wait.

func (*Login) Wait

func (l *Login) Wait(ctx context.Context) error

Wait blocks until the browser login finishes, then saves the token.

type Page

type Page[T any] struct {
	Items []T `json:"items"`
	Total int `json:"total"`
}

Page is one page of results.

type Playlist

type Playlist struct {
	URI    string  `json:"uri"`
	Name   string  `json:"name"`
	Images []Image `json:"images"`
	Owner  struct {
		DisplayName string `json:"display_name"`
	} `json:"owner"`
	// The February 2026 API renamed "tracks" to "items". Both are read.
	Tracks *struct {
		Total int `json:"total"`
	} `json:"tracks"`
	Items *struct {
		Total int `json:"total"`
	} `json:"items"`
}

Playlist is a Spotify playlist.

func (Playlist) TrackCount

func (p Playlist) TrackCount() int

TrackCount returns the number of tracks, or -1 if unknown.

type RateLimitError

type RateLimitError struct {
	RetryAfter time.Duration
	Reason     string
}

RateLimitError means Spotify rejected the call because of its rate limit.

func (*RateLimitError) Error

func (e *RateLimitError) Error() string

type SearchResult

type SearchResult struct {
	Tracks    Page[Track]     `json:"tracks"`
	Albums    Page[Album]     `json:"albums"`
	Artists   Page[Artist]    `json:"artists"`
	Playlists Page[*Playlist] `json:"playlists"`
}

SearchResult holds the results of a search. Spotify sometimes puts null entries in the playlist results, so they are pointers.

type Track

type Track struct {
	URI        string   `json:"uri"`
	Name       string   `json:"name"`
	DurationMs int      `json:"duration_ms"`
	Artists    []Artist `json:"artists"`
	Album      Album    `json:"album"`
}

Track is a Spotify track.

Jump to

Keyboard shortcuts

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