Skip to content

k.net.sftpClient ​

SFTP file upload and download

Execution ContextPrerequisitesTypical Use
KScript API, background jobsReachable SFTP host with password or PEM private keySyncing exports, importing backups, and transferring remote files

k.net.sftpClient shares the same connection pattern as sshClient. Once connected, perform file operations via ConnectedClient inside the callback; the connection closes automatically when the callback completes.

Quick Start ​

js
k.api.post("syncReport", () => {
  k.net.sftpClient.passwordConnect("sftp.example.com", "deploy", "password", (client) => {
    const report = k.file.readBinary("exports/report.csv")
    client.upload(report, "/incoming/report.csv")
  }, 30)
  return { uploaded: true }
})

Quick Reference ​

Method / PropertyDescriptionReturns
passwordConnect(host, user, password, callback[, timeout])Connect to an SFTP host using password authenticationvoid
privateKeyConnect(host, user, privateKey, callback[, timeout])Connect to an SFTP host using a PEM private key stringvoid
client.upload(data, path)Upload a byte array to a remote path inside the callbackvoid
client.download(path)Download a remote file as a byte array inside the callbacknumber[]

passwordConnect() / privateKeyConnect() ​

passwordConnect(host, user, password, callback, timeout?) connects with a password; privateKeyConnect(host, user, privateKey, callback, timeout?) connects with a PEM private key string. Operate on files inside the callback; the connection closes after the callback finishes.

ParameterTypeRequiredDescription
hoststringYesSFTP host name or IP
userstringYesLogin user
passwordstringRequired for passwordConnectLogin password
privateKeystringRequired for privateKeyConnectPEM private key content
callback(client) => voidYesCallback after the connection succeeds
timeoutnumberNoConnection timeout in seconds; default is 30

Returns: void. Use ConnectedClient inside callback to upload or download files.

js
k.net.sftpClient.passwordConnect(
  "sftp.example.com",
  "deploy",
  "password",
  (client) => {
    const report = k.file.readBinary("exports/report.csv")
    client.upload(report, "/incoming/report.csv")

    const remoteCopy = client.download("/incoming/report.csv")
    k.file.writeBinary("exports/report.remote-copy.csv", remoteCopy)
  },
  30
)

When using key authentication, load the PEM private key from a non-public server-side store and pass it as a string:

Never store private keys in k.file

By default, Kooboo sites have EnableFileIOUrl = true, meaning files stored via k.file are publicly downloadable by anonymous HTTP clients at /__kb/kfile/<path> (unless EnableFileIOUrl is disabled or denyAnonymousFileIOAccessRule is configured). Store PEM private keys in a non-public store such as k.DB.keyValue (up to 4096 UTF-8 bytes per entry), k.DB.sqlite / k.DB.indexedDb, or server-only configuration, and never expose them via public endpoints.

js
const keyEntry = k.DB.keyValue.get("sftp_deploy_key")
const privateKey = keyEntry ? keyEntry.toString() : ""
if (!privateKey) {
  throw new Error("Missing SFTP private key")
}

k.net.sftpClient.privateKeyConnect(
  "sftp.example.com",
  "deploy",
  privateKey,
  (client) => {
    const bytes = client.download("/exports/latest.csv")
    k.logger.information("SFTP", `Downloaded ${bytes.length} bytes`)
  }
)

ConnectedClient ​

Remote file session object passed into the connection callback.

MethodDescription
upload(data, path)Upload bytes to a remote path
download(path)Download a remote file as a byte array
ParameterTypeRequiredDescription
datanumber[]Required for uploadBytes to upload
pathstringYesRemote file path

Returns: upload() returns void; download() returns number[].

js
let lineCount = 0
k.net.sftpClient.passwordConnect(host, user, password, (client) => {
  const bytes = client.download("/exports/orders.csv")
  const text = k.utils.bytesToString(bytes, "utf-8")

  lineCount = text.split("\n").length
})
return { lineCount }

TypeScript Definitions ​

ts
interface SftpClient {
  passwordConnect(host: string, user: string, password: string, callback: (p1: ConnectedClient) => void, timeout?: number): void;
  privateKeyConnect(host: string, user: string, privateKey: string, callback: (p1: ConnectedClient) => void, timeout?: number): void;
}

interface ConnectedClient {
  upload(data: number[], path: string): void;
  download(path: string): number[];
}