Skip to content

Your first script ​

In about five minutes you will create a note, update it safely and move it to the trash, all from a script.

Before you start ​

  1. In PalmNotes open Settings → Developers and create a token with the scopes notes:read and notes:write.
  2. Keep it in an environment variable called PALMNOTES_TOKEN. Never paste it into code or commit it.

The script ​

js
// first-script.mjs (Node 22+)
// Run: PALMNOTES_TOKEN=pn_pat_… node first-script.mjs
const BASE = 'https://notes-api.palmcode.cl/api/v1';
const auth = { Authorization: `Bearer ${process.env.PALMNOTES_TOKEN}`, 'Content-Type': 'application/json' };

async function api(path, init = {}) {
  for (let attempt = 0; attempt < 4; attempt++) {
    const res = await fetch(BASE + path, { ...init, headers: { ...auth, ...init.headers } });
    if (res.status === 429) { // rate limited: back off 1s, 2s, 4s…
      await new Promise((r) => setTimeout(r, 2 ** attempt * 1000));
      continue;
    }
    if (!res.ok) throw new Error(`${res.status} ${JSON.stringify(await res.json())}`);
    return res.status === 204 ? null : res.json();
  }
  throw new Error('Still rate limited, try again later');
}

const { data: note } = await api('/notes', {
  method: 'POST',
  body: JSON.stringify({ title: 'My first API note', content_markdown: '# Hello\n\nCreated from a script.', tags: ['api'] }),
});
console.log('created', note.id);

const { data: updated } = await api(`/notes/${note.id}`, {
  method: 'PATCH',
  headers: { 'If-Match': note.version },
  body: JSON.stringify({ content_markdown: '# Hello\n\nUpdated from a script.' }),
});
console.log('updated, new version', updated.version);

await api(`/notes/${note.id}`, { method: 'DELETE' }); // moves it to the trash
console.log('moved to trash');
python
# first_script.py (pip install requests)
# Run: PALMNOTES_TOKEN=pn_pat_… python first_script.py
import os, time, requests

BASE = "https://notes-api.palmcode.cl/api/v1"
HEADERS = {"Authorization": f"Bearer {os.environ['PALMNOTES_TOKEN']}"}

def api(method, path, headers=None, **kwargs):
    for attempt in range(4):
        r = requests.request(method, BASE + path, headers={**HEADERS, **(headers or {})}, **kwargs)
        if r.status_code == 429:  # rate limited: back off 1s, 2s, 4s…
            time.sleep(2 ** attempt)
            continue
        r.raise_for_status()
        return None if r.status_code == 204 else r.json()
    raise RuntimeError("Still rate limited, try again later")

note = api("POST", "/notes", json={"title": "My first API note", "content_markdown": "# Hello\n\nCreated from a script.", "tags": ["api"]})["data"]
print("created", note["id"])

updated = api("PATCH", f"/notes/{note['id']}", headers={"If-Match": note["version"]}, json={"content_markdown": "# Hello\n\nUpdated from a script."})["data"]
print("updated, new version", updated["version"])

api("DELETE", f"/notes/{note['id']}")  # moves it to the trash
print("moved to trash")

What it does ​

  1. Creates a note from Markdown and reads its id and version.
  2. Updates it sending the version as If-Match. If someone edited the note in the meantime the API answers 409 version_conflict instead of overwriting their work.
  3. Deletes it. By default this moves the note to the trash, so nothing is lost.
  4. Retries with backoff when the API answers 429 (see Errors & rate limits).

Next steps ​

  • Page through everything with the cursor, see Working with notes.
  • React to changes instead of polling with Webhooks.
  • Revoke the token in Settings → Developers when you no longer need it.