Deploying to Railway

Railway logo

Create a project Dockerfile

The first step is to create a Dockerfile for your project. If you're using a monorepo, place the Dockerfile in apps/indexers.


COPY ./src/* /app

ENTRYPOINT ["/nix/store/rh1g8pb7wfnyr527jfmkkc5lm3sa1f0l-apibara-sink-postgres-0.7.0/bin/apibara-sink-postgres"]

Notice that the base image is build from scratch (not an OS like Ubuntu), so the binary is in a location that depends on the build. For this reason we stick to a specific version and architecture.

When updating the image you also need to update the Docker ENTRYPOINT.

First pull the new version of the image.

docker image pull
2352c28bb4a7: Download complete
979824b603fa: Download complete

Then get the entrypoint path by inspecting the image.

docker image inspect | jq '.[].Config.Entrypoint'

Save and push to GitHub.

Create a new Railway service

From the Railway dashboard, create a new service deployment and connect your GitHub repository.

  • Change "root directory" to the folder containing your Dockerfile, e.g. /apps/indexers.
  • Builder should automatically switch to "Dockerfile". If not, just wait a few seconds until it does.
  • Change the "custom start command". Since this option changes the Docker's entrypoint, you will need to pass the full path to the apibara-sink-* executable. For example: /nix/store/rh1g8pb7wfnyr527jfmkkc5lm3sa1f0l-apibara-sink-postgres-0.7.0/bin/apibara-sink-postgres run /app/my-indexer.ts
    • If you're using a Railway managed Postgres, read the section below for additional options.
  • Change "restart policy" to "Always".
  • Set the AUTH_TOKEN environment variable to your DNA token.
  • Set any other relevant environment variable needed by your indexer.
  • Remember to whitelist environment variables used in your indexer with the --allow-env-from-env option or the ALLOW_ENV_FROM_ENV environment variable. See the environment variables page for more information.
  • Once deployed, you should see the indexer starting and running.

Connect to a Railway Postgres database

If you're connecting your indexer to a PostgreSQL database managed by Railway, you will need to add the following flag to your start command (between run and the indexer's path).


This is needed because the certificate issued by Railway does not play nicely with rustls.

Persisting state between restarts

The easiest way to persist state between restarts on Railway is by using the managed Redis.

  • Deploy a new Redis instance on Railway, and wait for it to become ready.
  • Update the following indexer's environment variables:
    • Set PERSIST_TO_REDIS to a reference to Redis' url: ${{Redis.REDIS_URL}}.
    • Set SINK_ID to an unique id for this indexer, e.g. my-indexer.

After redeploying, you will notice that the indexer will store its state (like latest indexed block) in the provided Redis instance.

Last modified
Edit on GitHub

Apibara is the fastest platform to build production-grade indexers that connect onchain data to web2 services.

© 2024 GNC Labs Limited. All rights reserved.