Automate SFTP file exchange with suppliers and partners
Short answer: Many suppliers, wholesalers, payment processors and marketplaces still exchange files by SFTP: a stock or price file arrives every morning, and your orders go back every hour. To automate it reliably, use SSH keys instead of passwords, run the transfer in non-interactive batch mode so a failure stops the job, upload under a temporary name and rename when complete, keep an archive, and alert someone when an expected file doesn't arrive. If partners connect to your server, give each one a locked-down SFTP-only account confined to its own folder.
Two directions, two setups
Before scripting anything, be clear about who connects to whom:
- You connect to the partner's server. Common with large suppliers and processors. You need a client job on your side, a key the partner has authorised, and their host key recorded so you know you're talking to the right server.
- The partner connects to yours. Common when a smaller supplier sends you files. You need an SFTP account on your server, confined and restricted, with the partner's public key.
Many businesses end up with both. Write down for each partner: host, port, username, folders, file names, schedule, and who to contact when it breaks.
Use keys, not passwords
Automated jobs can't type passwords, and storing one in a script is a risk. The OpenBSD sftp manual notes that batch mode lacks user interaction, so it should be used with non-interactive authentication. Generate a dedicated key pair for each partner connection, protect the private key with file permissions, and send only the public key to the partner.
When the partner connects to you, add their public key to the account's authorized_keys file with restrictions. The restrict option disables port, agent and X11 forwarding and terminal allocation, which an SFTP-only partner never needs.
Pin the partner's host key
The first connection to a new server shows its host key. Get the fingerprint from the partner through a separate channel and compare it, then store it in your known hosts file. With StrictHostKeyChecking set to yes, the ssh_config manual says SSH will never add host keys automatically and will refuse to connect if a host key changes, which protects you from connecting to an impostor. For a scheduled job that's the behaviour you want: a changed key should stop the job and trigger an alert, not be accepted silently.
Script transfers in batch mode
sftp -b reads commands from a file. Its manual is specific about errors: in batch mode, sftp aborts if commands such as get, put, rename, rm or cd fail, and you can prefix a command with - to ignore errors for that command only. That makes the exit status meaningful, so your scheduler can tell success from failure.
A typical download job:
- change to the partner's outgoing folder,
- download new files to a local incoming folder,
- move or delete them on the remote side only after a successful download, if the partner allows it.
A typical upload job:
- upload
orders-20261109-1400.csv.part, - rename it to
orders-20261109-1400.csvonce the upload completes.
The rename step matters. Many systems pick up files as soon as they appear. Uploading under a temporary name and renaming at the end means the partner never processes a half-written file. Agree the convention with them, because some systems watch for specific extensions.
Locking down accounts for incoming partners
If partners connect to your server, keep them away from everything else:
- Put SFTP partners in a dedicated group and use a
Matchblock insshd_config. - Use
ForceCommand internal-sftpin that block so the account can't run shell commands. - Use
ChrootDirectoryto confine each partner to its own directory. Thesshd_configmanual requires every component of that path to be owned by root and not writable by group or others, so create a writable subfolder such asuploadinside it for the partner's files. For SFTP-only sessions using the in-process server, no extra files are needed inside the chroot.
Give each partner its own account. Shared logins make it impossible to tell who sent what, and hard to revoke one partner's access.
File naming, archives and duplicates
Agree a naming pattern with a date and time, such as stock-YYYYMMDD-HHMM.csv, so files sort correctly and nothing overwrites anything. On your side:
- keep every received and sent file in a dated archive folder for a sensible period,
- record which files you've processed, so a re-sent file isn't imported twice,
- check the file isn't empty and has the expected header row before importing it.
If the partner provides checksums or row counts, compare them before processing.
Monitoring that catches silence
The most damaging failure is often no file at all: the supplier's export stopped and your shop has been selling stock that doesn't exist. Alert on:
- the job failing (a non-zero exit code),
- no new file arriving within the expected window,
- a file much smaller than usual or with no data rows,
- authentication or host key errors.
Send alerts to a person or team channel that someone actually watches, and include the partner name and file pattern.
An illustrative example
A garden supplies wholesaler in Tauranga receives a stock file from a manufacturer every morning and sends orders back hourly. The original setup used a password stored in a script and occasionally imported half-uploaded files. The developer switches to a dedicated key, records the manufacturer's host key, uploads orders as .part and renames them, and checks each stock file's header before import. A morning alert fires if no stock file has arrived by 7:30, so staff know before customers do.
FAQ
Is SFTP the same as FTPS?
No. SFTP runs over SSH. FTPS is FTP with TLS. Partners usually specify which they need.
Can we use a managed file transfer service instead?
Yes, if it supports the protocol and features your partners require. The same rules about keys, naming and monitoring apply.
How often should keys be rotated?
Agree a schedule with each partner and rotate immediately if a key or server may be compromised.
What if the partner insists on passwords?
Store the password in a secrets manager, not the script, and push for key-based access.
If a supplier or partner wants files exchanged by SFTP and you want it automated safely, tell us who you exchange files with.