Your First Mock Server

Create your first mock API server in 10 minutes. Step-by-step tutorial covering YAML configuration, HTTP endpoints, JSON responses, and testing. Perfect for beginners to start mocking APIs quickly.

In this tutorial, you'll create a simple but functional mock API server from scratch. By the end, you'll have a working mock server that responds to HTTP requests with JSON data.

What you'll learn

  • How to write a basic moclojer configuration file

  • How to define HTTP endpoints

  • How to structure JSON responses

  • How to start and test your mock server

What you'll build

A simple user API with these endpoints:

  • GET /users - List all users

  • GET /users/123 - Get a specific user

  • GET /health - Health check endpoint

Prerequisites

  • moclojer installed (see installation guide)

  • A text editor

  • Command line access

  • curl or a tool to make HTTP requests

Step 1: Create your configuration file

Create a new file called moclojer.yml in an empty directory:

What this does:

  • Creates one endpoint that responds to GET /health

  • Returns HTTP status 200 (success)

  • Sets the response content type to JSON

  • Returns a simple JSON object with status information

Step 2: Start your mock server

Open your terminal in the directory with moclojer.yml and run:

You should see output similar to:

Step 3: Test your first endpoint

Open a new terminal window and test your endpoint:

You should get this response:

🎉 Congratulations! You've created your first mock API endpoint.

Step 4: Add more endpoints

Now let's expand your API. Stop the server (Ctrl+C) and update your moclojer.yml file:

What's new:

  • /users endpoint returns a list of users

  • /users/1 endpoint returns details for a specific user

  • Each response includes different data structures (array vs object)

Step 5: Test your expanded API

Restart your server and test the new endpoints:

Step 6: Handle different HTTP methods

Let's add a POST endpoint to create users. Add this to your moclojer.yml:

Test it:

Step 7: Add error responses

Real APIs return errors sometimes. Let's add a 404 response:

Test it:

Understanding the configuration

Let's break down what you've learned:

Endpoint structure

Key concepts

  • Method: What HTTP verb this endpoint responds to

  • Path: The URL pattern to match

  • Status: HTTP status code (200=success, 404=not found, etc.)

  • Headers: HTTP response headers (like Content-Type)

  • Body: The actual response content

YAML tips

  • Use > for multi-line strings (like JSON)

  • Indentation matters - use 2 spaces consistently

  • Each endpoint starts with - endpoint:

📚 Learn more: See the complete YAML Format Guide for advanced syntax, best practices, and troubleshooting.

What you've accomplished

✅ Created a working mock API server ✅ Defined multiple endpoints with different HTTP methods ✅ Structured JSON responses ✅ Added error handling ✅ Tested everything with curl

Your complete configuration

Here's your final moclojer.yml:

Next steps

You now have a solid foundation! But your API is still static - it always returns the same responses. In the next tutorial, you'll learn how to make your responses dynamic based on the request data.

👉 Dynamic Responses Tutorial - Learn to use path parameters, query parameters, and templates

Need help?

See Also

Last updated

Was this helpful?