Skip to main content

Call the API as yourself

Reading and writing your own DFOS data from a terminal or a script takes a signing key you hold, and nothing else: no app registration, no consent screen. The key lives on your machine and signs each request. It is the closest thing DFOS has to a personal API key, except that no key acts as you by being copied into a request, and you can remove it any time in Settings.

This guide is for one person working with their own account. An app acting for other people uses Sign in with DFOS instead.

Set up once​

1. Install the CLI.

# macOS / Linux
curl -sSL https://protocol.dfos.com/install.sh | sh

# or Homebrew
brew install metalabel/tap/dfos

Other platforms and the full command reference are in the CLI documentation.

2. Create a vault. The CLI mints your key from a vault, so make one first:

dfos vault create <name>

It prints a 24-word recovery phrase once. Write it down and keep it offline: it is what restores the keys this vault mints if you lose this machine. Passing --no-vault to dfos keys add instead makes a standalone key that no phrase covers; Key custody says what that costs.

3. Add a signing key. In DFOS, open Settings → DFOS Protocol and press Add key under Signing keys. DFOS shows the command to run, with a code that is good for ten minutes. Copy it as shown:

dfos keys add api.dfos.com/<code>

The CLI makes a new key on this machine and shows six words. Compare them with the words in Settings, then approve the key there. Nothing is added until you approve it. See Adding one.

4. Find your DID. It is at the top of the same Settings pane, under Your DID, with a copy button: did:dfos: followed by 31 characters, 40 in all. A shorter did:dfos: id shown elsewhere in the app, 22 characters after the prefix, is not accepted by the API.

5. Bring your identity to this machine. Under Your DID, Sync to your CLI opens two commands to copy. Run them after you approve the key:

dfos peer add dfos https://relay.dfos.com
dfos identity fetch <your DID> --peer dfos --name <name>

The CLI signs only with a key your identity lists, and it reads that list from its own copy of your identity. Fetching after the approval is what puts the new key in that copy. If you add or remove a key later, run dfos identity fetch <name> --peer dfos again. The name you give here also works in place of your DID in --as and in the CLI commands that name an identity.

Make a signed call​

Register the API once, then call operations by their operationId:

dfos api add dfos api.dfos.com

dfos --as <your DID> api call dfos profile.getOwnProfile --profile identity
dfos --as <your DID> api call dfos posts.listPosts --profile identity --param space=<space> --param limit=20

The CLI reads the API's OpenAPI document and picks a proof the operation accepts. --as chooses the identity; --profile identity makes the call an identity proof signed with your key, with no credential sent. Without it, the CLI tries the alternatives in turn, so when your key is not usable it can spend a stored grant instead, or send an optional-auth route such as posts.listPosts anonymously and return only the public projection. With the flag, a missing key is an error rather than a thinner answer.

The response body is JSON on stdout. Everything else goes to stderr: the line naming who signed, the line naming how the call was authenticated, and any error. --quiet drops the first two.

To stop typing --as, set a standing default:

dfos config set default-identity <name|did>

From a script​

Run the CLI and parse stdout. The key never enters the script:

// my-posts.mjs
import { execFile } from 'node:child_process';
import { promisify } from 'node:util';

const run = promisify(execFile);
const ME = 'did:dfos:…';

const call = async (operation, params = {}) => {
const args = ['--as', ME, '--quiet', 'api', 'call', 'dfos', operation, '--profile', 'identity'];
for (const [name, value] of Object.entries(params)) args.push('--param', `${name}=${value}`);
const { stdout } = await run('dfos', args);
return JSON.parse(stdout);
};

const profile = await call('profile.getOwnProfile');
const posts = await call('posts.listPosts', { space: 'home', limit: 20 });

A refused call exits non-zero, so run rejects and the CLI's message is on the error's stderr.

What this key can do​

An identity proof has no space restriction, and it writes as well as reads: a script holding this key can post, comment, and upvote as you. See Writing.

For something that should only read, or only reach some spaces, get a grant instead:

dfos --relay dfos login <your DID> --host dfos

It lists the actions the API offers and asks which to request, and --spaces limits the grant to the spaces you name. Call with --profile delegated to spend that grant rather than your key. That flag limits the one request, not the script: a script that can reach your signing key can still make identity calls. A script meant to be read-only or limited to some spaces should run where only the grant and its login key are available. The trade-off is lifetime: a grant on this tier expires after 14 days, and you approve it again. See Local applications and How a credential lives.

Signing in your own code​

If you would rather not run the CLI, @metalabel/dfos-client/api-auth builds the proof. signApiIdentityRequest takes the method, host, exact path (query string included), body, a kid, and a signing function, and returns the proof; buildApiIdentityHeaders turns it into the Authorization header. A write also needs a jti, which generateJti() mints. The byte contract is DFOS API-AUTH.

The kid is <your DID>#<key id>, with the full 40-character DID itself, not a local name. dfos keys add prints the key id when the key is approved, and dfos keys list shows it.

You also need the key's private half. The CLI keeps keys in the OS keychain when one is reachable, and in plaintext files under ~/.dfos/keys/ otherwise: with DFOS_NO_KEYCHAIN=1 set, or on its own when the keychain check fails, as it usually does on headless Linux and in CI. The Keys: line of dfos keys list names the backend in use. The two never read each other, so a key you want to read from a file has to be added by a run that used the file store. The file layout is in the CLI documentation, and Key custody covers what a plaintext key costs. Such a file is a password.

When it fails​

What you seeWhat it means
no identity to sign withThe CLI has no identity selected. Pass --as, or set default-identity. dfos whoami reports this as Identity: none selected.
identity '…' not found in local relayThe CLI has no copy of that identity. Run step 5, and check the DID is the 40-character one.
no held auth key for …This machine holds none of the keys your identity lists. Approve the key in Settings, then fetch your identity again.
presenter identity is not held by this platformThe DID in the kid is not one DFOS holds. When it goes on to say the kid does not name a protocol DID, that DID is not in the protocol shape, most often because the 22-character id was used. Use the one under Your DID.
identity proof signing key is not a current key of the presenterThe key id in the kid is wrong, or the key was presented but never approved in Settings.
identity proof is staleThe proof was signed too long before it arrived, or your clock is off. Sign at the moment you send. See Freshness.

The full table of statuses is When a gated request is refused.

For agents​

The CLI ships its own agent skill, matched to the installed version: dfos skill install --global installs it for Claude Code, and protocol.dfos.com/skill covers other agents.