DEV Community

Cover image for Spin up a License Key API on new Railway Functions In Less Than Two Minutes!
Doug silkstone
Doug silkstone

Posted on

Spin up a License Key API on new Railway Functions In Less Than Two Minutes!

Railway's new Functions feature makes it incredibly easy to deploy single-file TypeScript code without the overhead of managing infrastructure or repositories.

In this article, we'll walk through building a license API using Hono, Redis, and the Bun runtime—all in a single file. No more excuses, it's time to build software!

Note: Railway Functions are perfect for small tasks like handling webhooks, cron jobs, or simple APIs. What makes them even better is how they interact with the rest of the Railway ecosystem.

Customizing Your Prefixes

Before diving in, make sure to edit the prefix values in the code to suit your needs. The default prefixes, such as WTHSEISMIC_, are placeholders and should be replaced with values relevant to your project to ensure uniqueness and avoid conflicts.

Prerequisites

  • A Railway account.
  • Basic knowledge of TypeScript.
  • Familiarity with serverless concepts.

Step 1: Set Up a New Project

  1. Create a New Project:

    Head over to your Railway dashboard and create a new project. Choose the Functions service to get started.

  2. Select the Functions Environment:

    Railway will automatically set up the environment using the Bun runtime for your function. No need for additional configuration!

Step 2: Provision Redis

  1. Add a Redis Service:

    In your Railway project, provision a Redis service. This will be used to store license data.

  2. Share Environment Variables:

    After provisioning Redis, click on the Redis service, then head to Variables. Use the "Share Variables" feature to expose the following environment variables to your function:

  • REDISHOST
  • REDISPORT
  • REDISUSER
  • REDISPASSWORD
  • (Optional) REDIS_TLS if your Redis requires TLS.

Step 3: Create Your Function

  1. Create a New Function:

    In the Railway dashboard, click New Function. This opens the built-in editor which supports TypeScript using Bun.

  2. Set Up Additional Environment Variables:

    Add any additional variables your API requires. For example, generate unique values (e.g., using a UUID generator) for:

  • ADMIN_KEY
  • HMAC_SECRET
  • PAYMENT_SECRET
  • Optionally, set PORT (defaults to 3000 if not provided).

Step 4: Paste and Save the Code

Copy and paste the following complete code into the Source Code tab of your Railway Function. Save your changes using ⌘+S (or Ctrl+S on Windows). Railway will automatically deploy your changes!

// index.ts
import { Hono } from "hono";
import { cors } from "hono/cors";
import { rateLimiter } from "hono-rate-limiter";
import Redis from "ioredis";
import crypto from "node:crypto";

// Custom prefix configuration
const LICENSE_PREFIX = "WTHSEISMIC_";
const LICENSE_SET = "WTHSEISMIC_LICENSES";
const WEBHOOK_SET = "WTHSEISMIC_WEBHOOKS";

// Updated LicenseTier type to include "unlimited"
type LicenseTier = "basic" | "pro" | "enterprise" | "unlimited";

... (full code here)
Enter fullscreen mode Exit fullscreen mode

Step 5: Test Your Deployment

Once deployed, Railway provides a URL for your function. Use the following curl commands to test each endpoint:

5.1: Test the Root Endpoint

Returns the current timestamp.

curl -X GET https://<your-function-url>/
Enter fullscreen mode Exit fullscreen mode

5.2: Create a New License

Creates a new license key for a user.

curl -X POST https://<your-function-url>/api/licenses \
  -H "Content-Type: application/json" \
  -d '{"email": "user@example.com", "tier": "pro", "durationDays": 90}'
Enter fullscreen mode Exit fullscreen mode

5.3: Validate a License

Validates an existing license key.

curl -X GET https://<your-function-url>/api/licenses/WTHSEISMIC_xxx
Enter fullscreen mode Exit fullscreen mode

5.4: Handle Payment Webhook

Simulates a payment webhook to extend a license's duration.

curl -X POST https://<your-function-url>/webhooks/payment \
  -H "Content-Type: text/plain" \
  -H "X-Payment-Signature: <signature>" \
  -d '{"licenseKey": "WTHSEISMIC_xxx", "durationDays": 30}'
Enter fullscreen mode Exit fullscreen mode

5.5: Extend License via Admin Endpoint

Extends the license duration using an admin key.

curl -X POST https://<your-function-url>/admin/licenses/WTHSEISMIC_xxx/extend \
  -H "Content-Type: application/json" \
  -H "X-Admin-Key: your-secret-key" \
  -d '{"durationDays": 30}'
Enter fullscreen mode Exit fullscreen mode

Use these examples to verify that your function is working as expected.

So there you have it, in less than two minutes, we've put together a license key server on Railway using Redis, Hono and Bun - Demonstrating how headache-free the whole process is, and how (not so) small snippets of code can live within your Railway ecoystem within minutes.

Great feature, great platform.

Top comments (0)