Your first extension
Start an extension in one command, run it on your own server, and change it while it runs.
You need Node 22.12 or later, and a Songstress server you're an admin of.
Start one
Make the project
npm create songstress-extension@latestpnpm create, bun create and yarn create work too. It asks for a folder, a name, an id and a category, then
writes a working extension, starts a git repository and installs.
The id is publisher.name, like acme.play-count, and never changes. Use your GitHub user or organisation as the
publisher.
Look around
| File | What it is |
|---|---|
songstress.json | The manifest. It passes songstress check as it is |
src/server.ts | The server entry: it hears each play, keeps a count for each member, and answers a button on its settings page |
src/server.test.ts | Its tests, against a mock Songstress that refuses what a real server would |
tsconfig.json | The compiler held to the sandbox, so fetch, process and the DOM don't compile |
.github/workflows/ci.yml | Checks every push: the types, the tests, the manifest, the package |
.github/workflows/release.yml | Releases a tag as a GitHub release that carries the package |
README.md, CHANGELOG.md, LICENSE, AGENTS.md | What a repository needs around it |
Run it on your server
SONGSTRESS_PASSWORD='your password' npx songstress dev --server http://music.local:8090 --email you@example.comsongstress dev builds the extension, sends it to your server as a development install, switches it on, grants
what its manifest asks, and shows it to you alone. It does it again each time you save a file, and prints the
extension's log as the server writes it. Instead of a password, you can set SONGSTRESS_TOKEN to an admin's session.
Try it
Play a song for a while. In the terminal, the log says who started what, then the count. Open Settings ▸ Extensions ▸ Play Count in Songstress and select Show my count.
What the code does
The manifest declares two hooks, a setting, a command and the menu that shows it, and asks for two permissions:
{
"$schema": "https://songstress.app/schema/extension/v1.json",
"manifestVersion": 1,
"id": "acme.play-count",
"name": "Play Count",
"version": "0.1.0",
"description": "Counts the songs each member plays, and tells them the count.",
"engines": { "songstress": "^0.1.0" },
"license": "MIT",
"categories": ["scrobbling-stats"],
"entry": { "server": "dist/server.js" },
"hooks": { "server": ["onPlay", "onScrobble"] },
"permissions": {
"listening.read": { "reason": "Hear what you play, to count it." },
"storage": { "reason": "Keep your count." }
},
"contributes": {
"settings": [
{ "key": "counting", "type": "boolean", "title": "Count my plays", "scope": "user", "default": true }
],
"commands": [{ "id": "show-count", "title": "Show my count", "run": "server" }],
"menus": { "settings/actions": [{ "command": "show-count" }] }
}
}The server entry gives each one its behaviour, by id:
import { songstress } from 'songstress/server'
/** Whether a member wants their plays counted: the "counting" setting. */
async function counting(userId: string): Promise<boolean> {
return (await songstress.settings.get<boolean>('counting', { userId })) !== false
}
// A song started in one of a member's apps. Live, and may be missed:
// good for "now playing", not for counting.
songstress.hooks.onPlay((song, play) => {
console.log(`${play.userId} started "${song.title}" by ${song.artist}`)
})
// A play counted: half the song, or four minutes of it, was heard.
// The reliable one. A play made offline arrives later, with its own time.
songstress.hooks.onScrobble(async (_song, play) => {
if (!(await counting(play.userId))) return
const member = { userId: play.userId }
const count = ((await songstress.storage.get<number>('count', member)) ?? 0) + 1
await songstress.storage.set('count', count, member)
})
// The "Show my count" button on the extension's settings page.
songstress.commands.register('show-count', async (_input, context) => {
const count = (await songstress.storage.get<number>('count', { userId: context.userId })) ?? 0
return { action: { type: 'toast', message: count === 1 ? '1 play counted.' : `${count} plays counted.` } }
})What to notice:
- Everything is registered at the top level, each time the entry starts. The count lives in storage, never in a variable.
- On the server one extension serves every member, so each call that concerns one names them:
{ userId }. - The command answers with an action, a toast, and the app shows it.
Next
- Add what you need: providers, pages and panels, a theme. Each kind is in the manifest.
- Test it against the mock host.
- Release it with a tag, and submit it to be listed.
To start a theme instead of code, add --theme: you get a manifest and a theme file with a dark and a light look,
and nothing to build. See The command line for every option.