The nightly export to your logistics partner stops working on a Monday morning. Nobody changed the script. The cause turns out to be HR: the engineer who wrote the job left on Friday, their account was disabled, and the API token baked into the CI pipeline went with it. The opposite failure is worse. The account stays active, the token keeps working for two years, and a copy of it sits in a forgotten .env file on a laptop that was never wiped. Good API token security for file transfer automation prevents both.
This post covers why tokens are the soft spot in most file transfer pipelines, how to separate machine identities from people, how to scope, store and rotate tokens, and what to do in the first hour after one leaks.
Why tokens are the weak point in file transfer automation
Interactive logins get a lot of attention: MFA, lockouts, passkeys. Automation gets a long random string pasted into a config file. That string usually has the same power as the human who created it, never expires, and is copied into more places than anyone tracks.
For file transfer specifically, the blast radius is large. A token that can call your file transfer API can typically do some combination of:
- Read everything - Download invoices, payroll exports, patient files or whatever else flows through your exchange folders.
- Write and overwrite - Drop a malicious or altered file into a partner's inbound folder, where an import job will trust it.
- Delete - Remove files before downstream jobs pick them up, which looks like a partner outage rather than an attack.
- Change configuration - Depending on what the token is allowed to do, alter users, links or workflows.
The common leak paths are boring, which is why they work: tokens committed to Git, printed in CI logs, pasted into a support ticket or chat, stored in shell history, or left in a container image layer. Treat every token as something that will eventually be seen by someone who should not see it, and design so that the damage is small and the fix is fast.
Service accounts vs personal tokens
The first fix costs nothing: stop running automation as people.
A personal token is tied to a human account. It inherits that person's permissions, changes when their role changes, and dies when they leave. It also makes the audit trail misleading, because every action the nightly job takes looks like the engineer did it by hand at 2 a.m.
A service account is an identity that exists only for a system. It has a clear owner (a team, not a person), a description of what it is for, and only the permissions that job needs. When you read the audit log, svc-erp-export uploading a file tells you exactly what happened.
A practical naming and ownership convention:
- One service account per integration -
svc-erp-export,svc-warehouse-ingest,svc-partner-acme-pull. Do not share one "automation" account across every job. - A team as owner - Record the owning team and an on-call contact, so a departure never orphans the account.
- One token per environment - Staging and production never share a token. A staging leak should not open production.
The same idea applies to SFTP. If a script connects over SFTP, give it its own user and its own SSH key pair rather than reusing a person's key. The SSH key rotation guide for SFTP partners covers the key side of this in detail.
Scope tokens to the job, not the account
Least privilege for tokens means asking two questions for every integration: which operations does it perform, and on which data?
A job that pulls order files from an inbound folder needs to list and read that folder. It does not need to delete, write elsewhere or manage users. A job that pushes nightly exports needs to write to one outbound folder. It does not need to read anything at all.
Write the scope down before you create the token:
Integration: svc-erp-export (owner: data-platform team)
Environment: production
Operations: upload, list
Location: /outbound/erp/
Rotation: every 90 days, next due 2027-02-23
Stored in: CI secret ERP_EXPORT_TOKEN, production environment only
This five-line record pays for itself the first time something goes wrong. It tells responders what a leaked token could have touched, which is the difference between "rotate one token and check one folder" and "assume everything is compromised".
If your platform cannot narrow a token to the operations you need, compensate elsewhere: a dedicated service account with access only to the relevant folder, short rotation intervals, and close monitoring of what that account does.
Storing and injecting tokens safely
A token should exist in exactly one authoritative place (a secrets manager or your CI system's encrypted secrets) and be injected into the process at runtime. It should never live in source code, a Dockerfile, or a config file in the repository.
In GitHub Actions, keep the token as an environment-scoped secret and pass it as an environment variable to the step that needs it:
jobs:
export:
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@v4
- name: Upload nightly export
env:
FILE_API_TOKEN: ${{ secrets.ERP_EXPORT_TOKEN }}
FILE_API_URL: ${{ vars.FILE_API_URL }}
run: ./scripts/upload-export.sh
In the script, read the token from the environment and keep it out of the command line, where it would show up in process listings and shell history:
#!/usr/bin/env bash
set -euo pipefail
: "${FILE_API_TOKEN:?FILE_API_TOKEN is not set}"
curl --fail --silent --show-error \
--header @<(printf 'Authorization: Bearer %s\n' "$FILE_API_TOKEN") \
--upload-file "exports/orders-$(date +%F).csv" \
"$FILE_API_URL/outbound/erp/"
Python follows the same rule. Read from the environment, fail loudly if missing, and never log the value:
import os
import sys
token = os.environ.get("FILE_API_TOKEN")
if not token:
sys.exit("FILE_API_TOKEN is not set")
headers = {"Authorization": f"Bearer {token}"}
# Log the integration name, never the header
print("Uploading as svc-erp-export")
Then add a safety net that catches the mistakes people will still make. A secret scanner in pre-commit and CI blocks most accidental commits:
# Scan the working tree and history for anything that looks like a secret
gitleaks detect --source . --redact
For more on wiring file transfers into build and deploy pipelines, see file transfer in DevOps pipelines.
Rotating tokens without downtime
Rotation only happens if it is cheap. If replacing a token means a coordinated outage, it will be postponed until it is forced on you by a leak. The way to make it cheap is the same overlap pattern used for SSH keys:
- Create a new token for the same service account with the same scope.
- Deploy the new token to the secret store and roll it out to every consumer.
- Verify that the next scheduled run, or a manual test run, succeeded with the new token. Check your logs and the audit trail, not just the exit code.
- Revoke the old token.
- Record the rotation date and the next due date.
The gap between steps 2 and 4 is where both tokens are valid. Keep it short (hours or a day), and never skip step 4. A rotation that leaves the old token alive has added a credential, not replaced one.
Pick a rotation interval you will actually keep. Ninety days is a common default for long-lived automation tokens. Rotate immediately, outside the schedule, when someone with access to the secret store leaves, when a token appears anywhere it should not (a log, a ticket, a screenshot), or when a laptop or CI runner with access is lost or compromised.
Responding to a leaked token
When a token leaks, speed matters more than certainty. The order:
- Revoke first, investigate second - Revoking a token that turns out to be safe costs one rotation. Waiting to be sure costs whatever the attacker does in the meantime.
- Issue a replacement - Use the rotation steps above, so the integration recovers quickly.
- Review what the token did - Use the scope record to know where to look, then check the audit log for that service account over the exposure window: uploads, downloads, deletes and configuration changes. A tamper-evident log matters here, because you need to trust that the history has not been edited. The audit logging guide for file transfers explains what to capture.
- Check the data paths - Look for unexpected files in inbound folders, missing files, or downstream imports that ran on content nobody recognizes.
- Close the leak path - Remove the token from the log, repository history or ticket where it appeared, and fix the process that put it there.
What about AI agents?
AI agents that read and write files raise the same questions with higher stakes, because their actions are less predictable than a cron job. Do not hand an agent a long-lived, broadly scoped API token copied from a human account. Prefer an integration built on delegated authorization such as OAuth, keep it disabled until someone decides it is needed, and give the agent access only to the folders its task requires. Our SFTP MCP guide walks through connecting an agent to file storage.
Common pitfalls
- One token for every job - When it leaks, every integration is exposed, and rotating it breaks all of them at once.
- Automation running as a person - The job breaks when they leave, and the audit trail blames them for actions they never took.
- Tokens in command-line arguments - They show up in
psoutput, shell history and CI logs. Pass them through environment variables or files. set -xin CI scripts - Bash tracing prints expanded variables, including your token. Disable tracing around authenticated calls.- Revoking the old token "later" - Later never comes. Revocation is part of the rotation, not a follow-up task.
- No owner on the service account - Nobody knows whether
svc-test-2is safe to delete, so it lives forever with production access. - Securing the token but not the admin console - Whoever can sign in as an admin can create new tokens. Protect admin accounts with MFA.
How FilePulse fits
FilePulse is a fully managed file transfer service hosted in the EU. Its REST API supports scoped API tokens and service accounts, so each integration can run under its own identity rather than a person's account. SFTP access is public-key only, which gives scripts the same "one identity, one key" model. Admin accounts can use MFA with TOTP, WebAuthn or passkeys, with account lockout after repeated failures, and secrets are encrypted with AES-256-GCM. FilePulse also keeps a tamper-evident, SHA-256 hash-chained audit log with signed checkpoints, which is what you want to rely on when reviewing an incident. For AI agents, the MCP server uses OAuth 2.1 and stays off until an admin enables it.
Next step: Start a 14-day free trial of FilePulse, no credit card required, create a service account for one of your existing jobs and write its five-line scope record before you issue the token.


