---
schema_version: "1"
id: 833b8f9b787f49cbbb7657d18d4e797c6e0a0ae5385dae98f9bfa77eed8e6952
title: Building, Implementing, and Testing a REST API
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/apikit/latest/apikit-4-implement-rest-api.md
canonical_url: https://docs.mulesoft.com/apikit/latest/apikit-4-implement-rest-api
source_site: docs.mulesoft.com
content_type: documentation
content_hash: f069d115c9aea3547acac7b1535ce3cb2a3bf504ff6bab9f696c0ef6ca558128
description: MuleSoft Documentation Site
release: 4.x
latest: true
---
---
product: APIkit
version: 4.x
is-latest-version: true
---

# Building, Implementing, and Testing a REST API

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

Follow this end-to-end workflow to design, implement, and test a REST API in Anypoint Studio. The
example relies on the public [{JSON} Placeholder](https://jsonplaceholder.typicode.com) service,
which returns mocked user JSON so you can build and validate flows without hosting a backend.

## Before You Begin

Ensure you have the following software installed:

- Anypoint Studio 7.1.4 or later

- Mule runtime engine 4.1.1 or later

## Design Your API Specification

Design your API specification with API Designer from Design Center, with the API Specification
Project tool from Anypoint Studio, or with a text editor of your choice:

> [!NOTE] Anypoint Studio does not support OAS 3.1. Design OAS 3.1 specifications in
> [Anypoint Code Builder](https://docs.mulesoft.com/anypoint-code-builder/).

1.  Create a JSON file including a list of users that serve as an example of what the service
    returns:

    \[ { "id": 1, "name": "Leanne Graham", "username": "Bret", "email": "Sincere@april.biz",
    "address": { "street": "Kulas Light", "suite": "Apt. 556", "city": "Gwenborough", "zipcode":
    "92998-3874", "geo": { "lat": "-37.3159", "lng": "81.1496" } }, "phone": "1-770-736-8031
    x56442", "website": "hildegard.org", "company": { "name": "Romaguera-Crona", "catchPhrase":
    "Multi-layered client-server neural-net", "bs": "harness real-time e-markets" } } \]

2.  Save it as `user-example.json`.

3.  Create a new blank file with the following API specification:

    ```yaml
    #%RAML 1.0
    title: placeholder
    version: 1.0
    /users:
      get:
        description: Retrieve a list of all the users
        responses:
          200:
            body:
              application/json:
                example: !include user-example.json
      /userbyid:
        get:
          description: Get information about a particular user
          queryParameters:
            id:
              description: Specify the id of the user you want to retrieve
              type: integer
              required: false
              example: 3
          responses:
            200:
              body:
                application/json:
                  example: |
                    [
                      {
                        "id": 3,
                        "name": "Clementine Bauch",
                        "username": "Samantha",
                        "email": "Nathan@yesenia.net",
                        "address":
                          {
                            "street": "Douglas Extension",
                            "suite": "Suite 847",
                            "city": "McKenziehaven",
                            "zipcode": "59590-4157",
                            "geo": { "lat": "-68.6102", "lng": "-47.0653" },
                          },
                        "phone": "1-463-123-4447",
                        "website": "ramiro.info",
                        "company":
                          {
                            "name": "Romaguera-Jacobson",
                            "catchPhrase": "Face to face bifurcated interface",
                            "bs": "e-enable strategic applications",
                          }
                      }
                    ]
    ```

4.  Save it as `api.raml`.

5.  In Studio, select **File** > **New** > **Mule Project**.

6.  In **Project Name**, type the name of your Mule project.

    - If you are importing your API specification from Exchange, select **Import a published API**,
      click the plus icon, and select **From Exchange**.

      ![\*Import a published API\* highlighted when clicking on plus to select the From Exchange option](/mulesoft/current/en-us/apikit/latest/_images/import-from-exchange.png)

    - If you are importing your API specification from a local file in your file system, select
      **Import RAML from Local File**.

      ![\*Import RAML from local file\* highlighted in the API implementation section](/mulesoft/current/en-us/apikit/latest/_images/import-from-local-file.png)

    - If you are importing your API specification from Design Center, select **Download RAML from
      Design Center**.

      ![\*Import from Design Center\* highlighted in the API implementation section](/mulesoft/current/en-us/apikit/latest/_images/import-from-design-center.png)

7.  Select **Scaffold flows from these API specifications** if not already selected.

8.  Click **Finish**.

    APIkit generates the necessary flows based on the API specification you imported.

9.  Run your APIkit project by clicking **Run** > **Run As** > **Mule Application**.

## Simulate API Calls

1.  In Anypoint Studio, locate the **APIkit Consoles** view and click **Open console** under the
    base point URL `http://localhost:8081/console/`.

    ![APIkit console tab displays the location of the basepoint URL in the window.](/mulesoft/current/en-us/apikit/latest/_images/apikit-consoles.png)

2.  Your default browser opens. The **API Console** shows the API summary and information about the
    project, such as the title, version, and supported protocols.

    ![API console version info](/mulesoft/current/en-us/apikit/latest/_images/apikit-console1.png)

3.  In **API Summary**, select `GET` for the `/users` resource.

    It opens a detail page of the resource showing, among others, the expected JSON example for an
    HTTP 200 response.

    ![APIkit response](/mulesoft/current/en-us/apikit/latest/_images/apikit-console3.png)

4.  In **API Summary**, expand `/userbyid`.

5.  Select `GET` for the `/usersbyid` resource.

6.  Click `Try it` to expand the view for sending a request.

    The **API Console** displays a section to add parameters and headers to try a request.

7.  Select **Show optional parameters** if not selected.

8.  Type `3` as a query parameter value for the `id` parameter and click `Send`.

    An HTTP 200 response confirms the successful request for the `/usersbyid` resource, which
    contains user information for the user with ID 3. This response also includes the body with a
    JSON example.

    ![Information for the user having id 3 highlighted](/mulesoft/current/en-us/apikit/latest/_images/apikit-console4.png)

## Add Business Logic to Your API Implementation

Modify the flows to return actual data from the JSON Placeholder service instead of the examples
from the API.

1.  Drag a new **Flow** component from the **Mule Palette**.

2.  Rename the new flow as `user_request_flow`.

3.  Drag an **HTTP Request** component to the flow process.

4.  In the properties editor of **Request**, in **General**, set the following HTTP request options:

    - In **Method**, leave the default `GET`.

    - In **Path**, set to `/users`.

    - On **Query Parameters**, select **Switch to expression mode** and paste the following
      DataWeave script:

      output application/java
      ---

      if (not isEmpty(attributes.queryParams)) { id : attributes.queryParams.id } else {}

5.  Delete **Transform Message** component in `get:\users:api-config` and
    `get:\users\userbyid:api-config` flows.

6.  Drag one **Flow Reference** for each flow in replacement of the removed **Transform Message**
    components.

7.  In the properties editor of **Flow Reference** components, set flow name as `user_request_flow`.

8.  Save changes.

## Test Your API Implementation

1.  In Package Explorer, right-click the project name and choose **Run As** > **Mule Application**.

2.  Open a browser and go to `http://localhost:8081/api/users`.

    The user information from the JSON Placeholder service is returned:

    \[ { "id": 1, "name": "Leanne Graham", "username": "Bret", "email": "Sincere@april.biz",
    "address": { "street": "Kulas Light", "suite": "Apt. 556", "city": "Gwenborough", "zipcode":
    "92998-3874", "geo": { "lat": "-37.3159", "lng": "81.1496"

        ...
        {
        "id": 10,
        "name": "Clementina DuBuque",
        "username": "Moriah.Stanton",
        "email": "Rey.Padberg@karina.biz",
        "address": {
          "street": "Kattie Turnpike",
          "suite": "Suite 198",
          "city": "Lebsackbury",
          "zipcode": "31428-2261",
          "geo": {
            "lat": "-38.2386",
            "lng": "57.2232"
          }

    ... \]

3.  Get information about the user having ID = 4 by going to
    `http://localhost:8081/api/users/userbyid?id=4`.

    \[ { "id": 4, "name": "Patricia Lebsack", "username": "Karianne", "email":
    "Julianne.OConner@kory.org", "address": { "street": "Hoeger Mall", "suite": "Apt. 692", "city":
    "South Elvis", "zipcode": "53919-4257", "geo": { "lat": "29.4572", "lng": "-164.2990" } },
    "phone": "493-170-9623 x156", "website": "kale.biz", "company": { "name": "Robel-Corkery",
    "catchPhrase": "Multi-tiered zero tolerance productivity", "bs": "transition cutting-edge web
    services" } } \]

Your REST API creation, implementation, and testing are complete.

## See Also

- [{JSON} Placeholder](http://jsonplaceholder.typicode.com)

- [Tutorial: Build an API from Start to Finish](https://docs.mulesoft.com/general/api-led-overview)

- [Create and Publish an API Specification or API Fragment with Design Center](https://docs.mulesoft.com/design-center/design-create-publish-api-specs)
