Using Railway API
spot-z
HOBBYOP

2 years ago

Hello, I'm new to railway. I am in the process of creating a GH action to turn off my services when I am not using them for testing to reduce cost. I followed the documentation and created a token in the project settings but when I do the test as mentioned here https://docs.railway.app/guides/public-api#execute-a-test-query with my generated token I get this response:

{
    "errors": [
        {
            "message": "Not Authorized",
            "locations": [
                {
                    "line": 1,
                    "column": 9
                }
            ],
            "path": [
                "me"
            ],
            "extensions": {
                "code": "INTERNAL_SERVER_ERROR"
            },
            "traceId": "2399845632845343687"
        }
    ],
    "data": null
}

Not sure what I am doing wrong. Could this be because I am on the hobby plan?

10 Replies

brody
EMPLOYEE

2 years ago

Plan type has no restrictions when it comes to what API calls you can make.

You queried for me - that's something you need to use an account token for, try again, but this time use an account token.


spot-z
HOBBYOP

2 years ago

@brody Thanks for the answer that helped. No I am trying to run the API to stop and start deployments so I can save money my query is like so

curl --location 'https://backboard.railway.app/graphql/v2' \

--header 'Authorization: Bearer <project token or account token>' \

--header 'Content-Type: application/json' \

--data '{"query":"mutation deploymentRestart($id: String!) {\n deploymentRestart(id: $id)\n}","variables":{"id":"<projectId>"}}'

I am getting the below error. I have tried with and account token and the project token I created and same error

{
    "errors": [
        {
            "message": "Not Authorized",
            "locations": [
                {
                    "line": 2,
                    "column": 3
                }
            ],
            "path": [
                "deploymentRestart"
            ],
            "extensions": {
                "code": "INTERNAL_SERVER_ERROR"
            },
            "traceId": "4983959447391147026"
        }
    ],
    "data": null
}

brody
EMPLOYEE

2 years ago

You can run a deployment restart on a project id, you need to provide a deployment id.

You would get the deployment id by first making a request to get the running deployment.


spot-z
HOBBYOP

2 years ago

From what I've been reading online Looks like there should be a way to turn off my applications and stopping deployments may not be the best way. Is there a way you can recommend? I have the sleep app config but my apps dont sleep for good reasons like ingress traffic, DB connections and connections to rabbitmq


brody
EMPLOYEE

2 years ago

Removing a deployment would be the only way to stop an application that's running on Railway.


spot-z
HOBBYOP

2 years ago

The deployment ID always changes right so in order to do this I would need to find the latest deploymentId for a service then stop it. I am trying to automate this via GH actions on a cron schedule. If there are any examples you can refer me to it would be of great help.


brody
EMPLOYEE

2 years ago

You are correct, you would need to make a query to find the latest deployment before you can remove it or eventually redeploy it.
Here is the query needed to find the deploys for a given service -

query deployments($first: Int, $after: String, $input: DeploymentListInput!) {
  deployments(input: $input, first: $first, after: $after) {
    edges {
      node {
        id
        status
        createdAt
        url
        canRedeploy
      }
    }
  }
}

And here are the variables -

{
  "input": {
    "projectId": "id_here",
    "environmentId": "id_here",
    "serviceId": "id_here"
  },
  "first": 10
}

All three ids can be easily obtained through the command pallet when you have your desired service open -

cmd / ctrl + k -> Copy - and you will see an option to copy the ids.


spot-z
HOBBYOP

2 years ago

This is very helpful! For the authentication would I need a project token or account token?


spot-z
HOBBYOP

2 years ago

I figured the account token will do and it worked. Thanks a lot for the help @brody


brody
EMPLOYEE

2 years ago

Project token may work, but I only tested that query with an account token before sending it.


Loading...