# Your first extension

> Start an extension in one command, run it on your own server, and change it while it runs.

Page: https://songstress.app/docs/develop/quickstart/

You need Node 22.12 or later, and a Songstress server you're an admin of.

## Start one [#start-one]

### 1. Make the project [#1-make-the-project]

```sh title="Terminal"
npm create songstress-extension@latest
```

`pnpm 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.

### 2. Look around [#2-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 |

### 3. Run it on your server [#3-run-it-on-your-server]

```sh title="Terminal"
SONGSTRESS_PASSWORD='your password' npx songstress dev --server http://music.local:8090 --email you@example.com
```

`songstress 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.

### 4. Try it [#4-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 [#what-the-code-does]

The manifest declares two hooks, a setting, a command and the menu that shows it, and asks for two permissions:

```json title="songstress.json"
{
  "$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:

```ts title="src/server.ts"
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 [#next]

- Add what you need: [providers](/docs/develop/providers/), [pages and panels](/docs/develop/interface/),
  [a theme](/docs/develop/themes/). Each kind is in [the manifest](/docs/develop/manifest/).
- [Test it](/docs/develop/testing/) against the mock host.
- [Release it](/docs/develop/publish/) 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](/docs/develop/cli/#create-songstress-extension) for every option.
