> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bijection.com/llms.txt
> Use this file to discover all available pages before exploring further.

# SFTP directory source

> Mount a read-only directory source on your app.

The SFTP app polls one flat directory and publishes the newest observed
version per path as a File and a metadata row. The installer passes the label
that source state, filenames and Files live under.

The mount's clock starts a due pass at its next ten-second turn. Each page
progresses through a continuation of the directory receiver's task.

## Mount and bind

`sftp({ data, provider, writer })` returns the app to place at a path of your
tree. Bind the directory with `sftpDirectory` and declare its credential with
`secret()`.

```ts app.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { app, label, secret } from "@bijectionhq/bijection";
import sftp, { sftpDirectory } from "@bijectionhq/app-sftp";

const drops = label();
const login = secret();
const directory = sftpDirectory("sftp.partner.example:22", login, drops);

export default app({ drops, login, directory, drop: sftp({ data: drops, provider: directory }) });
```

The mount declares its `operator` role, which clears `data`. Only the operator
calls `drop.source.*`, `drop.files.list`, `drop.files.get` and `drop.writes.*`,
so initialization, pause, resume and immediate polls stay with whoever holds
it. The mount owns the position, active pass and health facts, plus the file
rows. Each accepted page and its next position commit together.

Set the credential to JSON holding the SSH username, the server's pinned
OpenSSH public key, an absolute directory inside the SSH account's root and
one login. Then grant the role and start the source:

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
bijection secret set login --from-file login.json
bijection mutate drop.source.initialize '{}'
```

`drop.operator` is granted from code: `ctx.grant(drop.operator, { to, under })` in one of your
mutations ([Grants](https://docs.bijection.com/access/grants.md)).

```json theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
{
  "protocol": "sftp",
  "username": "drop-reader",
  "hostKey": "ssh-ed25519 <server-public-key>",
  "directory": "/incoming",
  "login": { "kind": "privateKey", "privateKey": "<OpenSSH private key>" }
}
```

An encrypted private key may include `passphrase`. A password login uses
`{ "kind": "password", "password": "…" }`. Give the account read permission
only on its dedicated directory and constrain it on the SSH server. Publish
files under immutable names using atomic renames so the app reads coherent
content. The driver pins the server key and checks
size and modification time on both the open handle and its name. SFTP v3
does not expose an immutable version ID. The driver hashes the bounded body
during discovery and checks the same digest against a full reread for each
range before releasing that range.

A scan covers at most 2,048 directory entries and resumes strictly after the
last committed path. Files up to 1 MiB are assembled from 24 KiB ranges.
The app holds unfinished parts as private Files at the mount label, then
retires them on publication, failure, deadline, pause or reset. The File owner
accounts for each part and final File before writing it to storage. Staged
parts use the label's ordinary File quota, including its retirement horizon.
Larger files receive metadata only. Observations never infer deletion from absence.
The source implementation has yet to be compiled or tested.

Use the revision returned by `source.initialize` or `source.health` for
`poll`, `pause` and `resume`. A stale revision refuses `RevisionMoved`. A reset also takes the observed
`gapAt`.

The same mount accepts [FTP over TLS](https://docs.bijection.com/integrations/native-ftp.md),
an [SMB disk share](https://docs.bijection.com/integrations/native-smb.md), a [filesystem bridge](https://docs.bijection.com/integrations/native-filesystem.md),
a [WebHDFS gateway](https://docs.bijection.com/integrations/native-webhdfs.md), or an
[exact Git commit](https://docs.bijection.com/integrations/native-git.md) through their binders.

## Put a file

Bind a writer with `sftpWriter(endpoint, credential, label)` at the mount's
`data` label and pass it as `writer`; without it the mount has no `writes`.
Set the writer credential with the same server-pin and directory fields as the
reader. Use a dedicated confined account and pre-create a private, non-symlink
`.bijection` staging directory beneath the installed directory. Keep its
parent path and staging directory under trusted server control. The server
must support SFTP v3 no-overwrite rename from staging into the directory.

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import sftp, { sftpDirectory, sftpWriter } from "@bijectionhq/app-sftp";

const writeLogin = secret();
const writer = sftpWriter("sftp.partner.example:22", writeLogin, drops);
// in the tree: { writeLogin, writer, drop: sftp({ data: drops, provider: directory, writer }) }
```

The operator calls `drop.writes.put` and `drop.writes.get`. Send a command
key and `command: { name, data }`, where `name` is one flat ASCII filename and
`data` holds up to 24 KiB of canonical base64-encoded bytes. A repeated key
with identical inputs replays; with other inputs it refuses `KeyReused`, and a
name that is not one flat filename refuses `InvalidFilename`. The mount's
clock submits a single `once` task per command key.

If the task refuses, `drop.writes.get` reports `refused` with the
refusal value in `refusal`.

On `acknowledged`, the server acknowledged writing and closing the staged
file and renaming it to the requested name. Evidence carries the name and
the byte length and SHA-256 of the bytes sent. The hash is a local witness
of those bytes. Remote durability, later content and downstream processing
require separate evidence. The put creates a new name. A rename refusal or
a lost reply can settle the task `Unknown` and, after a connection loss, a
temporary file in the recipient’s staging directory. The put advances no
read cursor.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.