Skip to Content
Documentation

The host API

What your extension's code can call, on which side, with which permission, and the rules every call follows.

The API is one object, songstress, imported from the entry the code is written for:

src/server.ts
import { songstress } from 'songstress/server' // the server entry

songstress/client is the client entry's, and songstress alone is what both have. The API reference has every type.

The namespaces

The server entry's songstress:

Prop

Type

The client entry's:

Prop

Type

Rules for every call

  • Registration gives back a disposable. Every on… and every register returns an object with dispose(). Everything is disposed for you when the extension stops.
  • Calls to the host answer with a promise. Registration is the exception: it is immediate.
  • One member at a time. On the server one extension serves every member, so a call that concerns one names them: songstress.settings.get('token', { userId }), songstress.storage.set(key, value, { userId }). In an app the member is the one signed in and is never named. A member's part of the store is the part their own client code sees.
  • Failure has a code. A call that fails rejects with a SongstressError whose code is one of: no-host (the code isn't running inside Songstress, as in a test without the mock host), permission-denied, not-available (this side, app or build can't do it), limit-exceeded (a quota, a size, a rate or a deadline), invalid (a wrong argument, or something that doesn't exist), failed.
  • The player is asked, not commanded. player and queue requests are carried out as a tap on the app's own controls would be. Adding to the queue never touches what is playing.

Every call and its permission

Read from the SDK's own record of its API:

CallEntryPermission
songstress.hooks.onPlayboth, server, clientlistening.read
songstress.commands.registerboth, server, clientNone
songstress.ui.pages.registerboth, server, clientNone
songstress.ui.panels.registerboth, server, clientNone
songstress.ui.status.registerboth, server, clientNone
songstress.settings.getboth, server, clientNone
songstress.settings.onChangeboth, server, clientNone
songstress.storage.getboth, server, clientstorage
songstress.storage.setboth, server, clientstorage
songstress.storage.deleteboth, server, clientstorage
songstress.storage.keysboth, server, clientstorage
songstress.net.fetchboth, server, clientnetwork
songstress.host.infoboth, server, clientNone
songstress.hooks.onScrobbleserverlistening.read
songstress.hooks.onFavoriteserverlistening.read
songstress.hooks.onPlaylistChangeserverlibrary.read
songstress.hooks.onLibraryChangeserverlibrary.read
songstress.hooks.onScanCompleteserverlibrary.read
songstress.ui.notifyserver, clientNone
songstress.ui.refreshserver, clientNone
songstress.secrets.getserversecrets
songstress.secrets.setserversecrets
songstress.secrets.deleteserversecrets
songstress.providers.search.registerserverproviders
songstress.providers.lookup.registerserverproviders
songstress.providers.links.registerserverproviders
songstress.providers.lyrics.registerserverproviders
songstress.providers.metadata.registerserverproviders
songstress.providers.artwork.registerserverproviders
songstress.providers.artistInfo.registerserverproviders
songstress.providers.scrobble.registerserverproviders
songstress.providers.recommend.registerserverproviders
songstress.providers.play.registerserverproviders
songstress.providers.download.registerserverproviders
songstress.jobs.registerserverjobs
songstress.jobs.startserverjobs
songstress.routes.registerserverroutes
songstress.signIn.registerserversecrets
songstress.library.getSongserverlibrary.read
songstress.library.getAlbumserverlibrary.read
songstress.library.getArtistserverlibrary.read
songstress.library.getPlaylistserverlibrary.read
songstress.library.getAlbumSongsserverlibrary.read
songstress.library.getPlaylistSongsserverlibrary.read
songstress.library.searchserverlibrary.read
songstress.users.getserverusers.read
songstress.users.listserverusers.read
songstress.hooks.onPauseclientlistening.read
songstress.hooks.onTrackChangeclientlistening.read
songstress.hooks.onSeekclientlistening.read
songstress.hooks.onVolumeChangeclientNone
songstress.hooks.onQueueChangeclientlistening.read
songstress.hooks.onProgressclientlistening.read
songstress.commands.executeclientNone
songstress.player.getStateclientlistening.read
songstress.player.playclientplayer.control
songstress.player.pauseclientplayer.control
songstress.player.nextclientplayer.control
songstress.player.previousclientplayer.control
songstress.player.seekclientplayer.control
songstress.queue.getclientlistening.read
songstress.queue.addclientplayer.control

The network

net.fetch(url, request) is the only way out, to the hosts your network permission names (and, from the server, the network.local targets). The address a name resolves to is checked when connecting and on every redirect.

src/server.ts
import { songstress } from 'songstress/server'

const answer = await songstress.net.fetch('https://api.tunelog.example/v1/me', {
  method: 'GET',
  headers: { accept: 'application/json' },
  timeoutMs: 10_000,
})
if (answer.status === 200) console.log(JSON.parse(answer.body))

Prop

Type

Prop

Type

Storage and secrets

storage keeps your extension's own data, as JSON, under a quota the host sets. secrets keeps credentials sealed: only the server entry reads them, and no app ever sees one. On the server, pass { userId } to keep something as one member's.

src/server.ts
import { songstress } from 'songstress/server'

declare const userId: string

await songstress.storage.set('waiting', [], { userId })
const keys = await songstress.storage.keys('charts/')
await songstress.secrets.set('token', 'from the service', { userId })

Prop

Type

Prop

Type

Jobs

A job is work on the server, shown in Activity while it runs. Declare it, and give it every (15m, 6h, 1d) to run by itself; without it a job runs only when your code starts it.

Prop

Type

src/server.ts
import { songstress } from 'songstress/server'

songstress.jobs.register('sync', async (run) => {
  const pages = 10
  for (let page = 1; page <= pages; page++) {
    run.progress(page, pages, `Page ${page} of ${pages}`)
  }
})

const runId = await songstress.jobs.start('sync')

Prop

Type

A run is handed:

Prop

Type

Routes

JSON routes for signed-in callers, under a prefix that belongs to your extension. Two routes may not answer the same method and path (ROUTE_DUPLICATE).

Prop

Type

src/server.ts
import { songstress } from 'songstress/server'

songstress.routes.register('chart', async (request) => {
  const top = (await songstress.storage.get<string[]>(`chart/${request.params.period}`, { userId: request.userId })) ?? []
  return { status: 200, body: { top } }
})

A handler is handed the request, and answers:

Prop

Type

Prop

Type

Sign-in

A sign-in to the service your extension reaches, which a person starts and ends in Songstress. It needs the server entry and the secrets permission.

Prop

Type

src/server.ts
import { ProviderFailure, songstress } from 'songstress/server'

const service = 'https://tunelog.example/oauth'

songstress.signIn.register({
  // Where the person signs in. The service sends them back to redirectUrl, with state as it was.
  start({ redirectUrl, state }) {
    const back = `redirect_uri=${encodeURIComponent(redirectUrl)}&state=${encodeURIComponent(state)}`
    return { url: `${service}/authorize?response_type=code&client_id=songstress&${back}` }
  },
  // What came back, traded for the account's token, which stays in secrets.
  async finish({ url, redirectUrl }) {
    const code = /[?&]code=([^&#]+)/.exec(url)?.[1]
    if (!code) throw new ProviderFailure('sign-in-required', { message: 'Tunelog sent no code back.' })
    const body = `grant_type=authorization_code&code=${code}&redirect_uri=${encodeURIComponent(redirectUrl)}`
    const headers = { 'content-type': 'application/x-www-form-urlencoded' }
    const answer = await songstress.net.fetch(`${service}/token`, { method: 'POST', headers, body })
    if (answer.status !== 200) throw new ProviderFailure('unavailable')
    const { token, name } = JSON.parse(answer.body)
    await songstress.secrets.set('token', token)
    return { account: name }
  },
  async signOut() {
    await songstress.secrets.delete('token')
  },
})

Prop

Type

Prop

Type

Prop

Type

  • start is handed redirectUrl, your server's own address for coming back, and state, a one-time value the service must hand back. It answers the url of the service's page. What finish will need (a code verifier) goes in secrets: another instance of the entry may run it.
  • finish is handed the url the person came back on, whole. It keeps the account's tokens in secrets and answers the account: the name shown beside "Signed in as".
  • signOut is optional. Songstress forgets the account whether it runs, fails or isn't there.

With scope: "user" (API 0.1.7), each member signs in to their own account from their own page of the extension, and start, finish and signOut are each handed that member's userId: keep their tokens with secrets.set('token', token, { userId }).

Songstress finishes only the sign-in whose state came back, once, within ten minutes of its start, and keeps the account's name and nothing else. A server with no public address can't be sent back to, and refuses the start. While there is no account, your other code throws sign-in-required.

The library and members

With library.read, the server entry reads the catalogue: a song, an album, an artist or a playlist by id, the songs of an album or a playlist, and a search. With users.read, it reads members' ids and display names, nothing else.

src/server.ts
import { songstress } from 'songstress/server'

const found = await songstress.library.search('Kind of Blue', { types: ['album'], limit: 5 })
const people = await songstress.users.list()

Prop

Type

Prop

Type

The player and the queue

In client code, with listening.read, player.getState() and queue.get() read this device's player. With player.control, play, pause, next, previous, seek(positionS) and queue.add(songIds, { next }) ask it to act.

Prop

Type

Prop

Type

On this page