> ## Documentation Index
> Fetch the complete documentation index at: https://docs.apinizer.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Application of JSON Transformation and Script Policy on Created Mock API

> Detailed scenario guide including Mock API creation on Apinizer, application of JSON Transformation and Script policies, API Proxy creation and testing steps.

In this scenario, the application of **JSON Transformation** and **Script** policies to a Mock API created on Apinizer will be tested.

The numbering in the diagram below corresponds to the **order of operations**.

* **API Client** makes a request to **Apinizer**.
* **Apinizer** makes a request to **Cars Mock API**.
* The **tip, yıl, fiyat** **keys** in the response returned from **Cars Mock API** are converted to **type, year, price key** expressions by applying the **JSON Transformation policy** and transmitted to Apinizer.
* The operation of the **Script policy** is **optional.** Because this policy works if there is any **Order By parameter** in the **request coming from the API Client**.
* **Response is given from Apinizer to API Client**.

<img src="https://mintcdn.com/apinizer/s1G0_sxxeD_lU0Dx/images/tutorials/mock-api-json-transformation-scenario.png?fit=max&auto=format&n=s1G0_sxxeD_lU0Dx&q=85&s=5d359488f0b69d9bd5cf564baf553390" alt="Scenario Flow" width="1000" data-path="images/tutorials/mock-api-json-transformation-scenario.png" />

## Creating Mock API

First, the design of the **Mock API** to be requested is performed.

For this, the **Mock API** option found in the **API Creator** menu under the **Development** menu is selected.

<img src="https://mintcdn.com/apinizer/s1G0_sxxeD_lU0Dx/images/tutorials/mock-api-menu.png?fit=max&auto=format&n=s1G0_sxxeD_lU0Dx&q=85&s=d1073dc90d78ffc146b7999434e294cb" alt="Mock API Menu" width="200" data-path="images/tutorials/mock-api-menu.png" />

Since no **Mock API** has been created before, the text **No records found** appears on the page.

Click the **Create** button in the top right corner and start creating **Mock API**.

<img src="https://mintcdn.com/apinizer/6d3coyPLAezC6iKa/images/tutorials/create-mock-api.png?fit=max&auto=format&n=6d3coyPLAezC6iKa&q=85&s=e588788cf522ec9f25bce3324fb6d9e0" alt="Create Mock API" width="1000" data-path="images/tutorials/create-mock-api.png" />

If the expressions on this screen are examined one by one,

* There are two different options for creating **Mock API**.
* When the **Blank** expression is selected, **Name** and **Description** expressions are present.
* The API's name is determined with the **Name** field.
* Descriptions for this API can be written with the **Description** expression.
* With the **Generate Crud Operations** expression, it is specified that **GET**, **POST**, **DELETE**, **PUT** operations will come ready in the Mock API to be created.

<img src="https://mintcdn.com/apinizer/uEpU4gYmDePkcYS7/images/tutorials/import-spec-option.png?fit=max&auto=format&n=uEpU4gYmDePkcYS7&q=85&s=3817e7d3c4af8bc49608cbce828047b6" alt="Import Spec Option" width="1000" data-path="images/tutorials/import-spec-option.png" />

* If the **Import Spec** option is selected, the page will be created as in the image below.
* With this expression, an existing **API** can be included in Apinizer.
* The options under the **API Spec Type** expression specify what type the API to be added is.
* **Import Type** specifies how the addition operation will be performed.

In this scenario, an empty **Mock API** will be created, so the "Blank" expression is selected and the relevant fields are filled as shown in the image.

Click the **Save and Next** button to save the operation and proceed to the next step.

<img src="https://mintcdn.com/apinizer/uEpU4gYmDePkcYS7/images/tutorials/mock-api-definition.png?fit=max&auto=format&n=uEpU4gYmDePkcYS7&q=85&s=f0d1dd6f58c5c9fa954c97743ea89e3e" alt="Mock API Definition" width="1000" data-path="images/tutorials/mock-api-definition.png" />

If the expressions on this screen are examined one by one,

* The name and description information belonging to the API is seen in the **Definition - API Information** field.
* In the **API Endpoints -Endpoints of the API** field, the **endpoints** that the **Mock API** has are located.
* A new endpoint can be added with the **Add** button found in this field, or changes can be made on existing endpoints by selecting them.

<img src="https://mintcdn.com/apinizer/s1G0_sxxeD_lU0Dx/images/tutorials/response-body-edit.png?fit=max&auto=format&n=s1G0_sxxeD_lU0Dx&q=85&s=b62cfbf83b95947d43af63dd0e312862" alt="Response Body Edit" width="1000" data-path="images/tutorials/response-body-edit.png" />

<Info>
  In this scenario, tests will be performed through an endpoint named "/getCarList" of type "GET".
</Info>

If the fields on the page that will appear after selecting the relevant endpoint are examined;

* The type of endpoint to be created is determined with the **HTTP Method** field.
* The endpoint's name is specified in the field right next to it.
* A description can be added to this endpoint with the **Description** expression.
* The responses to be received when a request is made are determined with the **Response Information - Enter Response Details** field.
* New responses can also be created with the button containing "+" in this field.
* In this scenario, a **response** design that only returns **"200"** response will be performed.
* The button containing a pencil icon enables editing operations of the relevant response to be returned.

<img src="https://mintcdn.com/apinizer/6d3coyPLAezC6iKa/images/tutorials/create-api-proxy-button.png?fit=max&auto=format&n=6d3coyPLAezC6iKa&q=85&s=cde518a4bec747c9a23cdd24e77b30fa" alt="Create API Proxy Button" width="1000" data-path="images/tutorials/create-api-proxy-button.png" />

If the expressions on this screen are examined one by one,

* The response to be received when making a request to the endpoint is specified in **Response Body**.
* A list with type, year, and price is found in this field.
* The type of response to be returned is determined with the **Status Code** field, and the type of content to be returned is determined with the **Response Content Type** field.
* If a **Headers** parameter will be sent in the response, it is specified in the **Headers** field.
* If a condition is desired to be created in this response, a rule definition can be made from the **Condition** field.
* After these changes, click the **Edit** button to save the edits.

<img src="https://mintcdn.com/apinizer/6d3coyPLAezC6iKa/images/tutorials/api-proxy-settings-mock.png?fit=max&auto=format&n=6d3coyPLAezC6iKa&q=85&s=d28cb1e9f94da92e35718051de413438" alt="API Proxy Settings" width="1000" data-path="images/tutorials/api-proxy-settings-mock.png" />

Click the **Save** button in the bottom right corner to save the changes.

An API Proxy is also created from Mock API with the **Create API Proxy** button in the top right corner.

<img src="https://mintcdn.com/apinizer/uEpU4gYmDePkcYS7/images/tutorials/deploy-proxy-mock.png?fit=max&auto=format&n=uEpU4gYmDePkcYS7&q=85&s=48057fbe62a5c5b48e90d24c2af2eaa6" alt="Deploy Proxy" width="1000" data-path="images/tutorials/deploy-proxy-mock.png" />

## Creating API Proxy from Mock API

The proxy's usage and category are determined.

A suitable **Relative Path** is given to the proxy and the **Save** button in the top right corner is clicked.

<img src="https://mintcdn.com/apinizer/Q2GnWLLPNjT0-SZY/images/tutorials/test-endpoint-mock.png?fit=max&auto=format&n=Q2GnWLLPNjT0-SZY&q=85&s=57b9024b85fdb1b2cee7e726d6e2ee34" alt="Test Endpoint" width="1000" data-path="images/tutorials/test-endpoint-mock.png" />

Click the **Deploy** button and the **proxy** is deployed.

<img src="https://mintcdn.com/apinizer/s1G0_sxxeD_lU0Dx/images/tutorials/response-without-policy.png?fit=max&auto=format&n=s1G0_sxxeD_lU0Dx&q=85&s=2cc2b8cb52214beda5e6159408b5f468" alt="Response Without Policy" width="1000" data-path="images/tutorials/response-without-policy.png" />

The **/getCarList** endpoint under the **Develop** menu is selected and the **Test Endpoint** button is clicked.

<img src="https://mintcdn.com/apinizer/6d3coyPLAezC6iKa/images/tutorials/add-policy-json-transformation.png?fit=max&auto=format&n=6d3coyPLAezC6iKa&q=85&s=e17ced033e80f493343b0c0d18b3c627" alt="Add Policy" width="1000" data-path="images/tutorials/add-policy-json-transformation.png" />

<Info>
  In the first test to be performed, no policy application will be made in any way.
</Info>

When a request is made to the address in the URL field, **JSON data** in the **Response** field is seen as a response.

If the returned response is examined, it returns a list containing **"tip"**, **"yil"**, and **"fiyat"** **key** expressions.

<img src="https://mintcdn.com/apinizer/s1G0_sxxeD_lU0Dx/images/tutorials/select-json-transformation.png?fit=max&auto=format&n=s1G0_sxxeD_lU0Dx&q=85&s=b5e92201077366dc139fdfd52b0d0e45" alt="Select JSON Transformation" width="1000" data-path="images/tutorials/select-json-transformation.png" />

## Adding JSON Transformation Policy

A policy must be added to enable the **key** expressions in the scenario to change.

This policy will be in the field that will go as a response from the API to the Client.

Click the **Add Policy** expression in the field seen in the image below.

<img src="https://mintcdn.com/apinizer/uEpU4gYmDePkcYS7/images/tutorials/json-transformation-settings.png?fit=max&auto=format&n=uEpU4gYmDePkcYS7&q=85&s=3eda928b90f1bfc6a3297c0294b26804" alt="JSON Transformation Settings" width="1000" data-path="images/tutorials/json-transformation-settings.png" />

On the opened screen, the **JSON Transformation** policy marked in the image below is selected.

<img src="https://mintcdn.com/apinizer/s1G0_sxxeD_lU0Dx/images/tutorials/response-with-json-transformation.png?fit=max&auto=format&n=s1G0_sxxeD_lU0Dx&q=85&s=d31dcf3cf9278c40c3bf48f54bcd4dde" alt="Response With JSON Transformation" width="1000" data-path="images/tutorials/response-with-json-transformation.png" />

If the expressions on this screen are examined;

* A description can be added to the policy to be used with the **Description** field.
* The **Jolt Value** required for the message to be changed is written with the **Configuration - Enter the policy configurations** field.
* The written configuration is tested with the **Test Jolt** expression.
* The created policy is saved by clicking the **Save** button.
* Click the **Redeploy** button for the changes to take effect.
* Proceed to testing the selected endpoint.

<img src="https://mintcdn.com/apinizer/6d3coyPLAezC6iKa/images/tutorials/add-script-policy.png?fit=max&auto=format&n=6d3coyPLAezC6iKa&q=85&s=8e4a3e6cd8852a4ba5f21deca75cecbd" alt="Add Script Policy" width="1000" data-path="images/tutorials/add-script-policy.png" />

As seen in the image below, when a request is made to the specified **endpoint**, it is seen that the **key** expressions in the response returning from **Mock API** have changed.

<img src="https://mintcdn.com/apinizer/s1G0_sxxeD_lU0Dx/images/tutorials/script-policy-settings.png?fit=max&auto=format&n=s1G0_sxxeD_lU0Dx&q=85&s=1368dc726af57857833f850a28d1dc02" alt="Script Policy Settings" width="1000" data-path="images/tutorials/script-policy-settings.png" />

## Adding Script Policy

Next is the execution of the **script** that will sort according to the "Order By" parameter value to be sent from the client.

For this, the **Script policy** must be added to the field where the response sent from Mock API to the client is located.

Click the **Add Policy** expression and select the **Script policy** from here.

<img src="https://mintcdn.com/apinizer/s1G0_sxxeD_lU0Dx/images/tutorials/policy-order.png?fit=max&auto=format&n=s1G0_sxxeD_lU0Dx&q=85&s=a24a2def6312095451aed4ca4ef4d672" alt="Policy Order" width="1000" data-path="images/tutorials/policy-order.png" />

If the expressions on this screen are examined one by one,

* The programming language with which the **Script** to be created will be written is selected with the **Script Language** field.
* The **Script Body** field contains both the field where the script will be written and ready functions that facilitate use when writing scripts.
* In this scenario, the function named **ResponseBodyTextToClient** will be used.
* To get information about other ready functions, you can click [this link](/en/develop/policies/script).
* After writing the script to be used, click the **Save** button and the policy is saved.

<img src="https://mintcdn.com/apinizer/Q2GnWLLPNjT0-SZY/images/tutorials/test-without-order.png?fit=max&auto=format&n=Q2GnWLLPNjT0-SZY&q=85&s=612c9748cceda67a53a390401ba7db15" alt="Test Without Order" width="1000" data-path="images/tutorials/test-without-order.png" />

After the last additions on the proxy, it is **Redeployed**.

First, the **JSON Transformation** policy must be run and then the **Script** policy must be run.

For this, the icon belonging to the **JSON Transformation** policy must be moved to the end. The usage desired to be created is shown in the image below.

<img src="https://mintcdn.com/apinizer/Q2GnWLLPNjT0-SZY/images/tutorials/test-with-order-year.png?fit=max&auto=format&n=Q2GnWLLPNjT0-SZY&q=85&s=02410707cea6a5f41d8700e1af7927fa" alt="Test With Order Year" width="1000" data-path="images/tutorials/test-with-order-year.png" />

## Testing the API Proxy

Click the **Test Endpoint** expression.

In the first test, no query parameters are sent to the mock API and the answer in the "Response" field is obtained.

<img src="https://mintcdn.com/apinizer/Q2GnWLLPNjT0-SZY/images/tutorials/test-with-order-price.png?fit=max&auto=format&n=Q2GnWLLPNjT0-SZY&q=85&s=a87846218fc886325dbc14198056caf5" alt="Test With Order Price" width="1000" data-path="images/tutorials/test-with-order-price.png" />

Then, the **order=year** query parameter is added to sort the response to be received by year.

<img src="https://mintcdn.com/apinizer/5OaccE6GrE8Reusq/images/tutorials/mock1.png?fit=max&auto=format&n=5OaccE6GrE8Reusq&q=85&s=4aa0f2e7768a4deaccc16de314a8c66d" alt="Test With Order Price" width="1000" data-path="images/tutorials/mock1.png" />

Finally, the **order=price** query parameter is added to sort the response to be received by year.

<img src="https://mintcdn.com/apinizer/5OaccE6GrE8Reusq/images/tutorials/mock2.png?fit=max&auto=format&n=5OaccE6GrE8Reusq&q=85&s=ed8b438a8644c76d5005dc3acecac89a" alt="Test With Order Price" width="1000" data-path="images/tutorials/mock2.png" />
