---
title: "Automate Credential Rotation on Railway"
description: "How to automate credential rotation on Railway, including scripting API key rotation and using the databasePasswordReset mutation for database passwords."
date: 2026-11-20T18:54:53.715Z
updated: 2026-11-24T15:50:50.967Z
topics: ["CI/CD", "Security"]
tags: ["Environment Variables", "Secrets", "Automation", "Cron", "Ci Cd", "Deployment"]
url: https://railway.com/guides/automate-credential-rotation
---

# Automate Credential Rotation on Railway

**TL;DR**

- Railway doesn't rotate credentials automatically, so choose your own schedule and script the steps.
- For third-party API keys, a scheduled job issues the new key, updates the Railway variable, deploys, verifies, and revokes the old key.
- For Railway databases, use the `databasePasswordReset` mutation.

A **credential** is a secret that proves who you are to another system, such as an API key or a database password. This article covers how to script rotation so it runs on a schedule. For the manual steps behind each credential type, see [Rotate third-party API keys on Railway](https://railway.com/guides/rotate-third-party-api-keys) and [Rotate database credentials on Railway](https://railway.com/guides/rotate-database-credentials).

## How to rotate credentials on a schedule

Rotate credentials immediately if they may have leaked, someone who knew them leaves the team, or they appear in a log or commit. Otherwise, establish a rotation schedule appropriate to the credential and its provider.

Railway doesn't rotate credentials on a schedule, so you must automate the process yourself. To do this, create a scheduled automation, such as a cron service within the same Railway project, a CI/CD workflow, or an external job runner. The implementation depends on the type of credential.

### **Third-party API keys**

A scheduled job can automate rotation by calling the provider's API to issue a new key, updating the corresponding Railway variables, deploying the affected services, verifying that the new key works, and revoking the old key.

The job should revoke the old key only if verification passes. If any step fails, it should leave the old key in place to avoid disrupting your services.

You can update Railway variables using the CLI:

`railway variable set API_KEY=new_key --skip-deploys`

You can also call the Railway GraphQL API directly with an HTTP request:

```shell
curl --request POST \
  --url https://backboard.railway.com/graphql/v2 \
  --header "Authorization: Bearer $RAILWAY_TOKEN" \
  --header "Content-Type: application/json" \
  --data '{
    "query": "mutation($input: VariableUpsertInput!) { variableUpsert(input: $input) }",
    "variables": {
      "input": {
        "projectId": "<projectId>",
        "environmentId": "<environmentId>",
        "serviceId": "<serviceId>",
        "name": "API_KEY",
        "value": "new_key",
        "skipDeploys": true
      }
    }
  }'
```

See [Manage variables with the API](https://docs.railway.com/integrations/api/manage-variables).

### **Railway database passwords**

Use the `databasePasswordReset` mutation in the public API, which performs the same operation as clicking **Regenerate** in the dashboard. It changes the password inside the database, updates the password variable and anything that references it, and redeploys the services that reference the password.

Run the mutation with the Railway CLI:

```shell
railway api 'mutation($input: DatabasePasswordResetInput!) {
  databasePasswordReset(input: $input) {
    rotationInProgress
  }
}' --variables '{"input":{"serviceId":"<serviceId>","environmentId":"<environmentId>","databaseType":"postgres"}}'
```

The effect depends on how the database is deployed:

- **Standalone database:** The database is redeployed with the new password, causing brief downtime. Railway automatically redeploys services that use Railway variables; services using hardcoded passwords must be updated manually.
- **High availability cluster:** Members adopt the new password without restarting, ensuring connections remain available. However, a brief write interruption may occur if an image update on the primary node triggers a failover.

Do not use `railway variable set` or `variableUpsert` alone to rotate a database password, because they change only the variable and not the password in the database.

### **Externally managed databases**

If the database isn’t managed by Railway, your script must also change the password through the database provider. For the token your scheduled job uses to access Railway, see [Railway's API tokens](https://docs.railway.com/integrations/api).

# **FAQ**

**Which Railway token should my rotation job use?**

Use the narrowest token that works. A project token is limited to one environment, which usually fits. Store it as a sealed variable.

**Where should the rotation job run?**

Anywhere that can run on a schedule and reach both Railway and your provider, such as a cron service in the same project or a CI workflow.

## **Summary**

Railway doesn't rotate credentials for you, so script each step and pick a schedule that fits the credential. Rotate immediately if a credential may have leaked. Downtime can’t always be avoided, so schedule rotations of standalone databases and single-key providers for low-traffic periods.

See also:

- [Rotate third-party API keys on Railway](https://railway.com/guides/rotate-third-party-api-keys)
- [Rotate database credentials on Railway](https://railway.com/guides/rotate-database-credentials)
- [Rotate environment variables on Railway without downtime](https://railway.com/guides/rotate-environment-variables-without-downtime)
- [Manage variables with the API](https://docs.railway.com/integrations/api/manage-variables)
- [Managing Secrets on Railway](https://railway.com/guides/managing-secrets-on-railway)
- [Variables](https://docs.railway.com/variables)

