How to build and host (for free!) a mock API from hardcoded data
In this example, we are going to build an API that serves data. This will then be hosted as a REST API. For this, we are going to be using RAW, a platform to quickly build and host APIs. To follow along, you will need an account, which you can create and use for free here if you don't have one already.
Let's get started!
If you are familiar with RAW and want to deploy this endpoint on your account, click below:
Mock API using hardcoded data
- Overview
- Code
Sample usage:
/mock/api[?id=<identifier>]
For example, when called as /mock/api?id=1
the response is a JSON document such as:
{
"id": 1,
"name": "Stephen Curry",
"age": 34,
"championships": [2015, 2017, 2018, 2022]
}
... but when called with an unknown identifier, e.g. /mock/api?id=99
, the response is an error such as:
Unknown identifier
main(id: int = 1) =
if id == 1 then
{id: 1, name: "Stephen Curry", age: 34, championships: [2015, 2017, 2018, 2022]}
else if id == 2 then
{id: 2, name: "LeBron James", age: 37, championships: [2012, 2013, 2016, 2020]}
else
Error.Build("Unknown identifier")
Create a blank API endpoint in RAW
First, we should create a free account:
Once you login to RAW, you should head up to the 'Workspace' section as shown below.
Then, click on 'Add +' to create a new endpoint and choose a new 'Blank Endpoint' as shown below. You could also choose an existing template, but for this example, let's start blank and write the code ourselves.
Write the endpoint code
Now that you have a blank endpoint, let's start writing some code. Before we start, here's an overview of the RAW Workspace.
Now let's follow in steps:
Step 1: Write the code
In RAW, endpoints are written in Snapi, a simple-to-use programming language specifically created specifically for building APIs. You will see this is very simple to create.
Let's copy/paste the following code for our endpoint (see figure, as step '1'):
main(id: int) =
if (id == 1) then {id: 1, name: "Stephen Curry", age: 34, championships: [2015, 2017, 2018, 2022]}
else if (id == 2) then {id: 2, name: "LeBron James", age: 37, championships: [2012, 2013, 2016, 2020]}
else Error.Build("Unknown identifier")
// The following test will run if you press the play button.
main(1)
Don't worry if you don't follow all the code just yet. This will be explained in detail below.
Step 2: Test the code
Next, let's test the code. Click on the play button (shown in the figure as step '2') and you will have a live preview of the result of calling the last line of the code.
Step 3: Choose the final URL
You can choose the exact path where your API will be hosted. This is shown in the figure as step '3'.
Step 4: Edit the metadata
Optionally, you can edit the metadata metadata (shown in the figure as step '4'). The metadata is important since RAW includes a built-in API Catalog that helps you and your users find API endpoints later.
Step 5: Deploy the endpoint live!
We are almost done. Now click to deploy your endpoint (shown in the figure as step '5').
Congratulations, your API is now published! It will be served right away and visible in the API Catalog as well.
How does the code work?
Let's look closer at how the Snapi code works!
main(id: int) =
if (id == 1) then
{
id: 1,
name: "Stephen Curry",
age: 34,
championships: [2015, 2017, 2018, 2022]
}
else if (id == 2) then
{
id: 2,
name: "LeBron James",
age: 37,
championships: [2012, 2013, 2016, 2020]
}
else Error.Build("Unknown identifier")
// The following test will run if you press the play button.
main(1)
- Line 1 defines the
main
method. Its arguments will become query parameters in the URL call. In this case, there's a single argumentid
, which is a number and is mandatory. - Lines 2, 9, 16 define the conditions based on the identifier passed.
- The data is returned as part of each condition (e.g. Lines 3-8 and Lines 10-15). This uses Snapi records which are conceptually similar to JSON objects.
- Line 16 uses
Error.Build
, to build an error response that is returned to the user, in case an unsupported identifier is passed. Refer to the error handling for more information. - Line 19 defines the test to run when the play button is pressed.
This is the simplest way to create a mock API as it allows you to quickly defined the desired output. However, it requires you to manually defined the data values in Snapi code. It is adequate for returning rather trivial responses.
For large volumes of data, this is not recommended however. In that scenario, prefer to use mock APIs that return data from existing data sources or, for a more complex solution, using code that dynamically generates data e.g. using Int.Range
or Math.Random
as the basic constructs.
Let's improve this API!
Now that you understand the basic concepts, there's many improvements that can be done. Below is a list of pointers:
- To secure the endpoint, read this guide.
- To improve performance with caching, read this guide.
- Learn how to invite users.
- To create API keys, read this guide.
- If you prefer, paid plans have access to GitHub integration, which allow teams to collaboratively build code together, write test suites, and provides a complete CI/CD flow.
What's next!
Take a look at other examples, or join us in our Community to learn more!
Ready to try it out?
Register for free and start building today!Otherwise, if you have questions/comments, join us in our Community!