Building Dice Roller API
Introduction: What You'll Build and Why
Welcome! In this lesson, you'll build a simple Express application from scratch using Codex, focusing on creating a dice roller API. This API will let you roll any number of dice with any number of sides and return the results as JSON, making it a practical way to get hands-on with Express, TypeScript, and Codex. You'll start by setting up a new Express project with TypeScript, then add the core logic for rolling dice, and finally expose this functionality through a web API endpoint. By the end of the lesson, you'll have a working endpoint that you can test using the platform's preview feature, giving you a solid foundation in express API development before moving on to more advanced features like dice notation and roll history.
Project Setup: Starting From Scratch
Let's begin by creating a new Express application with TypeScript. If you're new to Express, it's a minimal web framework that makes building APIs straightforward.
To get started, you'll need Node.js 18+ installed. You'll use npm (Node Package Manager) to manage dependencies.
Here's how you can ask Codex to help you set up the project:
What happens here?
- Codex will generate commands to initialize a new Node.js project with
npm init. - It will install Express, TypeScript, and necessary type definitions.
- It will create a
tsconfig.jsonfile to configure TypeScript compilation. - It will set up a basic Express server in
src/index.tsthat listens on the correct port.
This sets up the basic structure you need to start building your app.
Adding Core Logic: The Dice Rolling Function
Next, let's add the core logic: a function that rolls dice. We want a function that takes two numbers — how many dice to roll (count) and how many sides each die has (sides) — and returns a list of random results.
You can ask Codex to help you with this:
Codex will create utils.ts inside your src folder with something like:
Explanation:
Math.random()generates a number between0and1.Math.random() * sidesscales it to the number ofsides.Math.floor()rounds down, and adding1gives us a range from1tosides(inclusive).- The function uses type annotations (
: number,: number[]) to ensure type safety. export constmakes this function available to other files.
This function is the heart of your Dice Roller.
Building The API Endpoint
Now, let's make this functionality available through a web API. We'll create an Express route that reads the count and sides from the URL's query parameters, uses your rollDice function, and returns the results as JSON.
Prompt Codex like this:
Example Codex output:
Explanation:
app.get('/roll', ...)defines a GET route that responds toGETrequests at/roll.req.querycontains the query parameters from theURL.parseInt()converts the string parameters to numbers.res.json()sends a JSON response to the client.- The server listens on
0.0.0.0:3000to accept connections from any network interface.
Running and Testing
Now it's time to see your work in action. First, make sure you have a script in your package.json to run TypeScript:
Then start the server with this command (important for the platform):
Explanation:
ts-nodecompiles and runs TypeScript files directly without a separate build step.- The server listens on
0.0.0.0:3000, which means it accepts connections on all network interfaces on port3000, which the platform exposes.
Once the server is running, open the platform's preview feature for port 3000 and test:
You should see a JSON response like:
This means you rolled 3 six-sided dice and got results 2, 5, and 4. The total is 11.
Summary And What's Next
In this lesson, you set up a new Express application with TypeScript using Codex's help, wrote a utility function to roll dice, and built an API endpoint that returns dice rolls as JSON. You also learned how to run your Express server on the platform using the correct command so it's accessible for testing. With these steps, you now have a working dice roller API that you can interact with through your browser or API tools. In the next lesson, you'll build a user-friendly landing page with an interactive interface for your dice roller.
