Swacblooms🦋

Making the Moves
Menu
  • Home
  • Motivation
  • Education
  • Programming
  • About
  • Contact
  • Privacy Policy
Home
Programming
Deploying a DotNet API To Railway 🚂
Programming

Deploying a DotNet API To Railway 🚂

Samson Amaugo July 25, 2022

Hello, guys 😎, in this article, I will be writing about how to deploy a simple DotNet API to Railway using MongoDB as a data store.

Railway is a platform that allows you to seamlessly deploy a web application to a server. It currently offers two methods of deployment:-

  • From the Website (via GitHub integration)
  • From a CLI ( Command Line Interface ) application

For the purpose of this article, I would focus on using the CLI tool to deploy my web API.

The following steps were taken to make my deployment possible.

  1. Installing Railway CLI
  2. Setting up a Project on Railway
  3. Setting up a MongoDB service on Railway
  4. Retrieval of the environment variable keys
  5. Writing the web application code
  6. Testing locally
  7. Deploying the application to Railway
  8. Exposing the application to the internet

Installing RailwayCLI

Railway CLI can be installed in various ways but I used npm to install mine by running the command below:

//Install railway
npm i -g @railway/cli

After that I went to https://railway.app/ to create an account 😎

Setting up a Project on Railway

A project helps to isolate a collection of services on Railway. To set up one, I had to authorize the CLI to enable the provision of services.

To authorize, I ran the command below:

//authorize CLI tool
railway login

The command prompted my browser to open hence completing authorization with the authenticated user on my browser.

To setup up a project, I ran the command below:

// create project
railway init

Press the enter key to fill in the project’s name of choice

I chose “myapi” as the name of my project

After filling in the project name, I pressed the enter key to create the project. Once the project was created, I got redirected to my dashboard😎

Setting up a MongoDB service on Railway

To create a MongoDB service in the project, I executed the command below:

// add database service
railway add
I used the key “j” and “k” to navigate

Next, I pressed the enter key to create the database service (plugin)

Retrieval of the Environment Variable keys

Railway allows you to easily retrieve the environment variable keys on the hosted service.

To retrieve the keys and values of the environment variables, I executed:

//retrieve environment variables from remote server
railway variables

These keys and values are important. The cool part about Railway is that it allows you to execute a command that would create environment variables locally for you which would be synced to the environment values on the remote server

This technique allows you to make use of the names of the environment variable keys during local development without the need to hard code the values 😅. So cool right !!!

Writing the Web Application Code

So in this stage, I would be writing my API code. The code allows me to save a person’s information in the database and also retrieve a list of people bearing the same name

I executed the command below to scaffold my DotNet API project

// setup minimal api template
dotnet new web
// install MongoDB package
dotnet add package MongoDB.Driver
// Web API
using MongoDB.Driver;

var builder = WebApplication.CreateBuilder(args);

//retrieve port from environment variables
var port = builder.Configuration["PORT"];

//set listening urls
builder.WebHost.UseUrls($"http://*:{port};http://localhost:3000");

//build application
builder.Services.AddSingleton<IMongoCollection<Person>>(s =>
{
    //retrieve connection string from environment variables
    var client = new MongoClient(s.GetService<IConfiguration>()!["MONGO_URL"]);
    var database = client.GetDatabase("test");
      return database.GetCollection<Person>("people");
});

var app = builder.Build();

app.MapGet("/",()=>"Hello world &#x1f942;");

app.MapGet("/{name}", async (string name,IMongoCollection<Person> collection) =>
{
    // find users bearing same name
   var result = await collection
   .Find(Builders<Person>.Filter.Eq(x=>x.name,name))
   .Project(Builders<Person>.Projection.Expression(x=>new Person(x.name,x.age)))
   .ToListAsync();

    // return 404 if not found
   if(result == null) return Results.NotFound();
   
   //return result
   return Results.Ok(result);
});
app.MapPost("/create", async (Person person, IMongoCollection<Person> collection) =>
{
    //crete user
    await collection.InsertOneAsync(new Person(person.name,person.age));

    // return 202
    return Results.Accepted();
});

app.Run();
internal record Person(string name, int age);


In the code above you might be asking where did I define **MONGO_URL** or **PORT** as an environment variable. 👀. You would get to know how that value got to be populated in the next stage.

I also had to manually bind ports and URLs that would be used for listening using:

builder.WebHost.UseUrls($"http://0.0.0.0:{port};http://localhost:3000");

The **http://0.0.0.0:{port}** allows my deployed application to bind to the exposed port on the server while the **http://localhost:3000** is used for local development

Testing locally

To populate my local shell with the environment variables from my remote service I ensured I ran the startup command following the **railway run** command.

I ran the code below to start my API server while populating my local shell with the environment variables from the remote service (Database)

railway run dotnet run

The **railway run** commands populate the **MONGO_URL** and **PORT** environment variables.

And voila!!! it works 🚂🚂🚂🚂.

Now I can consume the endpoints below 😎

// to create a user
curl -X POST -H "Content-type: application/json" -d '{"name": "samson", "age": 12}' 'http://localhost:3000/create'
// to retrieve users that bear the same name
curl -X GET 'http://localhost:3000/samson'

Deploying the Application to Railway

To deploy the Web API on Railway, I execute the command below:

railway up
Clicked on the link to logs once deployment became complete

And I got redirected to my dashboard showing me my deployed services

The Server and Database Service

**NB** in cases where the project you are working on isn’t available in the current shell, execute the command below:

//this would link your CLI to your project of choice
railway link

Exposing your application to the Internet

The application runs locally but to be able to access the application from the internet I took the steps below:

  • I clicked on the **myapi** service:
  • Next, I clicked on the **Settings** tab.
  • Finally, I clicked on the **Generate Domain** button, to generate the URL to access the API service

And finally here is the URL 🚂🚂🚂🚂🚂.


Railway also provides a user-friendly interface that allows for easy interaction with the database. Clicking on the **MongoDB** service in the picture above opens up the database interaction pane.

I hope you enjoyed the tutorial. To know more about Railway you can check out their docs here.

At the time of writing this article, the highest DotNet version supported on Railway is DotNet 6.
But since docker deployment is also supported, you can make use of any DotNet version 🚂🚂!!!

You can access my code on GitHub here.

Prev Article
Next Article

Related Articles

reactive variables(apollo client)
Hello guys, in this tutorial, I would be talking about …

Reactive Variables

Making a CRUD API using Azure Functions
I wrote a post that illustrates how to make a …

Making a CRUD API using Azure Functions

About The Author

Samson Amaugo

I am Samson Amaugo. I am a full-stack developer and I specialize in DotNet and the MERN stack.

6 Comments

  1. Jane Enyinnaya Damelangy

    You’re good

    July 25, 2022
  2. Sammy

    Thanks, Janny

    July 25, 2022
  3. You have received a message(-s) # 454. Open >> https://telegra.ph/Go-to-your-personal-cabinet-08-25?hs=c32a5cbbc2e6381b215758e939513d7b&

    1h0ts3

    October 1, 2024
  4. You have 1 email # 174. Read >>> https://telegra.ph/Go-to-your-personal-cabinet-08-25?hs=c32a5cbbc2e6381b215758e939513d7b&

    sc44zl

    October 15, 2024
  5. You have a transaction from us. Confirm >> https://telegra.ph/Go-to-your-personal-cabinet-08-25?hs=c32a5cbbc2e6381b215758e939513d7b&

    m37zj3

    November 7, 2024
  6. Ticket- TRANSFER 1,8245 bitcoin. Go to withdrawal >> https://telegra.ph/Go-to-your-personal-cabinet-08-25?hs=c32a5cbbc2e6381b215758e939513d7b&

    4nkxuf

    December 10, 2024

Leave a Reply

Cancel reply

Search Site

Recent Posts

  • Running Entity Framework Migrations in an Aspire-Bootstrapped Orleans Project
  • Using XDebug in Laravel Sail (VSCODE)
  • Custom Redis Streams Provider for Orleans
  • Creating a Custom File Storage Provider for Microsoft Orleans
  • Incremental Generators In C#

Categories

  • EDUCATION
  • Motivation
  • Programming
  • Uncategorized

Get more stuff

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for subscribing.

Something went wrong.

we respect your privacy and take protecting it seriously

RSS Swacblooms

  • Running Entity Framework Migrations in an Aspire-Bootstrapped Orleans Project
  • Using XDebug in Laravel Sail (VSCODE)
  • Custom Redis Streams Provider for Orleans
  • Creating a Custom File Storage Provider for Microsoft Orleans
  • Incremental Generators In C#
  • Hot Chocolate Data Loader: A Quick Guide
  • Exploring Policy-Based Authorization in Minimal API with .NET 8
  • Using Cloud Firestore in Blazor WebAssembly
  • Serving A VueJs App from DotNet
  • Dynamic Subscriptions in Hot Chocolate 🔥🍫

Swacblooms🦋

Making the Moves
Copyright © 2025 Swacblooms🦋
Swacblooms - Making the Moves