Help using railway API
Anonymous
HOBBYOP

4 months ago

Is it possible to check the latest deployment's status? like whether it's online or deployment crashed etc. And also how do I get the second latest deployment data?

$10 Bounty

3 Replies

Anonymous
HOBBYOP

4 months ago

N/A


smolpaw
HOBBY

4 months ago

Railway provides a Graphql API to query deployment status.
More on this here: https://docs.railway.com/reference/public-api

Here's a curl request to get the deployment status of a service:

curl --silent --request POST --max-time 60 \
    --url https://backboard.railway.com/graphql/v2 \
    --header "Project-Access-Token: 7539e5f9-xxxx-yyyyyy-zzzzz" \
    --header 'Content-Type: application/json' \
    --data '{"query":"query { deployments(first: 2, input: { projectId: \"f652d67c-1928-41e2-93e8-xxxxxxxxx\", environmentId: \"ac58fe83-a9a1-40ff-9706-xxxxxxxxxx\", serviceId: \"87fb9248-696d-49a6-896d-xxxxxxxx\" }) { edges { node { id status } } } }"}'

This was the response

{
    "data": {
      "deployments": {
        "edges": [
          {
            "node": {
              "id": "509cfc82-9792-49e4-bd78-xxxxxxxxxxx",
              "status": "SUCCESS"
            }
          },
          {
            "node": {
              "id": "054ab7b6-f201-4eab-a3fc-xxxxxxxxxxx",
              "status": "REMOVED"
            }
          }
        ]
      }
    }
}

You need to got into your project and create a project token.
You can get the projectId, environmentId and serviceId by going into your service and clicking the "X Railway provided variables available".


smolpaw
HOBBY

4 months ago

There are the possible status values:

BUILDING
CRASHED
DEPLOYING
FAILED
INITIALIZING
NEEDS_APPROVAL
QUEUED
REMOVED
REMOVING
SKIPPED
SLEEPING
SUCCESS
WAITING

jitmisra
HOBBY

4 months ago

Checking the Latest Deployment's Status

You can query the API for your project's deployments, ordering them by creation date and limiting the result to the most recent one. The status field will tell you if it's online, crashed, or in another state.

The possible statuses include: SUCCESS, BUILDING, DEPLOYING, FAILED, CRASHED, REMOVED, and THROTTLED.

GraphQL Query

Here's the query to get the status of the single latest deployment:

GraphQL

query latestDeploymentStatus {
  project(id: "YOUR_PROJECT_ID") {
    deployments(first: 1) {
      edges {
        node {
          id
          status
          createdAt
        }
      }
    }
  }
}

How it works:

  • project(id: "YOUR_PROJECT_ID"): You target your specific project. You can find your Project ID in the URL of your project dashboard: https://railway.app/project/YOUR_PROJECT_ID/...

  • deployments(first: 1): This fetches the deployments, and first: 1 ensures you only get the most recent one since deployments are returned in reverse chronological order by default.

  • status: This is the key field that gives you the current state of the deployment.

Getting the Second Latest Deployment Data

To get the second latest deployment, you simply adjust the query to fetch the two most recent deployments. The second item in the returned list will be the one you're looking for.

GraphQL Query

This query retrieves the two most recent deployments.

GraphQL

query secondLatestDeployment {
  project(id: "YOUR_PROJECT_ID") {
    deployments(first: 2) {
      edges {
        node {
          id
          status
          createdAt
          # You can request more data here
          # For example, staticUrl or the deployment's logs
          staticUrl
        }
      }
    }
  }
}

How it works:

  • deployments(first: 2): This fetches the two most recent deployments.

  • The result in edges will be an array containing two objects. The first object (edges[0]) is the latest deployment, and the second object (edges[1]) is the second latest deployment.

How to Make the API Request

You'll need to send a POST request to Railway's GraphQL endpoint with your API token.

  1. Get Your API Token: Go to your Railway account settings, navigate to the Tokens tab, and generate a new token.

  2. Find Your Project ID: Go to your project's dashboard and copy the ID from the URL.

You can use any HTTP client. Here is an example using cURL:

Bash

curl 'https://backboard.railway.app/graphql/v2' \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer YOUR_RAILWAY_API_TOKEN' \
  --data-binary '{"query": "query { project(id: \"YOUR_PROJECT_ID\") { deployments(first: 2) { edges { node { id status } } } } }"}'

Replace YOUR_RAILWAY_API_TOKEN and YOUR_PROJECT_ID with your actual credentials. This will give you the JSON response containing the data for your two most recent deployments.


Loading...