Build your own SFTP Server using NodeJS

Markus Springer
10 min readAug 2, 2020

NodeJS is awesome isn’t it? You can build basically everything you can imagine, stuff you need stuff you don’t need and stuff you’ve never thought about.

SFTP — Creating a problem that never existed before.

The task was to provide for each customer an own SFTP server with own credentials, tenant based, with backups, fast file transfer, TB of files, scalable…

After some googling I found a neat website which lists all popular SFTP server which are ready to be spin up.

Open SSH Server → sounds like a good idea, so let’s give this a try.

But how do you spin up a SFTP server for each client, how do you manage the shutdown, how do you make sure that no resources are wasted, how do you manage authentication on application level? These were all things I didn’t consider before. So I dig deeper and I found a super cool project by mscdex.

His library provides an easy way to implement the SFTP protocol with NodeJS with the SSH protocl.

This is awesome!

SSH2 client and server modules written in pure JavaScript for node.js.

Development/testing is done against OpenSSH (7.6 currently).

So let’s get started with the basic implementation with Typescript:

yarn add ssh2
yarn add @types/ssh2 --dev

That’s a basic implementation which spins up an ssh server.

import fs from "fs";
import {AuthContext, ClientInfo, Connection} from "ssh2";
import * as ssh2 from "ssh2";

new ssh2.Server({
// Use absolute path here
hostKeys: [fs.readFileSync('/User/ma.springer/.ssh/id_rsa')],
banner: "This is our server",
}, (client: Connection, info: ClientInfo) => {
console.log(`Connection requested by ${info.ip}`);
client.on('authentication', (ctx: AuthContext) => {
console.log("Authentication works");
Markus Springer

Senior Software Engineer at Celonis #decacorn with a passion for learning new technologies. #founder #celonaut #engineer #fullstack