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:
import { songstress } from 'songstress/server' // the server entrysongstress/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 everyregisterreturns an object withdispose(). 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
SongstressErrorwhosecodeis 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.
playerandqueuerequests 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:
| Call | Entry | Permission |
|---|---|---|
songstress.hooks.onPlay | both, server, client | listening.read |
songstress.commands.register | both, server, client | None |
songstress.ui.pages.register | both, server, client | None |
songstress.ui.panels.register | both, server, client | None |
songstress.ui.status.register | both, server, client | None |
songstress.settings.get | both, server, client | None |
songstress.settings.onChange | both, server, client | None |
songstress.storage.get | both, server, client | storage |
songstress.storage.set | both, server, client | storage |
songstress.storage.delete | both, server, client | storage |
songstress.storage.keys | both, server, client | storage |
songstress.net.fetch | both, server, client | network |
songstress.host.info | both, server, client | None |
songstress.hooks.onScrobble | server | listening.read |
songstress.hooks.onFavorite | server | listening.read |
songstress.hooks.onPlaylistChange | server | library.read |
songstress.hooks.onLibraryChange | server | library.read |
songstress.hooks.onScanComplete | server | library.read |
songstress.ui.notify | server, client | None |
songstress.ui.refresh | server, client | None |
songstress.secrets.get | server | secrets |
songstress.secrets.set | server | secrets |
songstress.secrets.delete | server | secrets |
songstress.providers.search.register | server | providers |
songstress.providers.lookup.register | server | providers |
songstress.providers.links.register | server | providers |
songstress.providers.lyrics.register | server | providers |
songstress.providers.metadata.register | server | providers |
songstress.providers.artwork.register | server | providers |
songstress.providers.artistInfo.register | server | providers |
songstress.providers.scrobble.register | server | providers |
songstress.providers.recommend.register | server | providers |
songstress.providers.play.register | server | providers |
songstress.providers.download.register | server | providers |
songstress.jobs.register | server | jobs |
songstress.jobs.start | server | jobs |
songstress.routes.register | server | routes |
songstress.signIn.register | server | secrets |
songstress.library.getSong | server | library.read |
songstress.library.getAlbum | server | library.read |
songstress.library.getArtist | server | library.read |
songstress.library.getPlaylist | server | library.read |
songstress.library.getAlbumSongs | server | library.read |
songstress.library.getPlaylistSongs | server | library.read |
songstress.library.search | server | library.read |
songstress.users.get | server | users.read |
songstress.users.list | server | users.read |
songstress.hooks.onPause | client | listening.read |
songstress.hooks.onTrackChange | client | listening.read |
songstress.hooks.onSeek | client | listening.read |
songstress.hooks.onVolumeChange | client | None |
songstress.hooks.onQueueChange | client | listening.read |
songstress.hooks.onProgress | client | listening.read |
songstress.commands.execute | client | None |
songstress.player.getState | client | listening.read |
songstress.player.play | client | player.control |
songstress.player.pause | client | player.control |
songstress.player.next | client | player.control |
songstress.player.previous | client | player.control |
songstress.player.seek | client | player.control |
songstress.queue.get | client | listening.read |
songstress.queue.add | client | player.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.
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.
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
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
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
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
startis handedredirectUrl, your server's own address for coming back, andstate, a one-time value the service must hand back. It answers theurlof the service's page. Whatfinishwill need (a code verifier) goes insecrets: another instance of the entry may run it.finishis handed theurlthe person came back on, whole. It keeps the account's tokens insecretsand answers theaccount: the name shown beside "Signed in as".signOutis 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.
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