# 4. Execute Web Service Endpoints with Vault Methods

To execute any of the web service endpoints setup in the previous step (e.g. get all customers, get customer by Id, update customer, and delete customer), we must first write code in each of the connected integrations to open, read, and/or write to the **data** vault.

### Get All Customers

Log into Integration Gateway, if necessary and navigate to the **Build** page. Select the `get_all_customers` integration and add the following [code](https://glyue.docs.sandboxbanking.com/tutorials/building-a-restful-crud-web-service/5.-vault-code-examples-and-explanation#get-all-customers) in the **Before Hook**.

<figure><img src="https://688288018-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1flQ2To8tQpCQWl2Ty9U%2Fuploads%2F467EnsKCrVN5wpI7beZI%2Fimage.png?alt=media&#x26;token=3e896346-c75d-4a32-bf49-f5dd89f289f2" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
Assigning a value to `output.payload` allows you to provide the desired content for the response body.
{% endhint %}

Navigate back to the **Swagger** page. Select the web service endpoint `/rest/get_all_customers`*.* Click on the **Try it out** button and the **Execute** button.

<figure><img src="https://688288018-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1flQ2To8tQpCQWl2Ty9U%2Fuploads%2FY8zJx48CLobv9IyBZT9n%2Fimage.png?alt=media&#x26;token=151164dc-93e0-4b37-8bf1-ea20a82b5aa9" alt=""><figcaption></figcaption></figure>

You should receive text in the response body confirming that there are no customers in the **data** vault.

<figure><img src="https://688288018-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1flQ2To8tQpCQWl2Ty9U%2Fuploads%2FMytnqwbNw6R9PRAikNAk%2Fimage.png?alt=media&#x26;token=96911e86-e4db-4943-b2b3-2e39aaaf45de" alt=""><figcaption></figcaption></figure>

On the **Run History** page, you would noticed the integration `get_all_customers` was executed because it is the integration connected to the endpoint `/rest/get_all_customers`. Also, the `input` displays the HTTP method and the full path or web service endpoint that was used to call this integration.&#x20;

<figure><img src="https://688288018-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1flQ2To8tQpCQWl2Ty9U%2Fuploads%2FWBjqCodLqRHHVFAShI7J%2Fimage.png?alt=media&#x26;token=7882d4f2-984b-4916-ab2f-1a58bad279d9" alt=""><figcaption></figcaption></figure>

In the next step, we will execute this web service endpoint again after we add a new customer in the **data** vault.&#x20;

### Add a Customer

In the `add_customer` integration, we will create a service request and several field mappings to insert a customer in the **data** vault. The customer information will come from the body of a request and will contain the customer's first name, last name, birth date, and social security number.

Select the `add_customer` integration to highlight it and right click it to display a context menu.&#x20;

On the context menu, hover over **Go To** or **Go to (New Tab)** and select **Service Request.**&#x20;

<figure><img src="https://688288018-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1flQ2To8tQpCQWl2Ty9U%2Fuploads%2FLf0Er4EBkCuZgogbk1gg%2Fimage.png?alt=media&#x26;token=494a1364-457c-4a40-953a-e17d74eb6fdb" alt=""><figcaption></figcaption></figure>

You will be redirected to the **Service Request** component on the **Build** page. On this page, click on the **Add Row** button at the bottom of the screen and fill in the fields with the following information for this service request.&#x20;

* **Sequence** - 1
* **System** - ECHO
* **Service Name** - N/A
* **Formula Variable** - insert\_customer

Click on the **Save** button below.&#x20;

<figure><img src="https://688288018-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1flQ2To8tQpCQWl2Ty9U%2Fuploads%2FkA4zjX844eFsH7fNsz8r%2Fimage.png?alt=media&#x26;token=3566086a-19a5-403e-b56f-ee727c78c7a2" alt=""><figcaption></figcaption></figure>

Click on the service request to highlight it and right click it to display a context menu again. On the context menu, hover over **Go To** or **Go to (New Tab)** and select **Field Mapping**.

You will be redirected to the **Field Mapping** component on the **Build** page. On this page, click on the **Add Row** button at the bottom of the screen and fill in the fields with the following information.

* **Sequence** - 1
* **Field** - id
* **Value** - import uuid; retvalue = uuid.uuid4()
* **Value Type** - str

Click on the **Save** button below.&#x20;

<figure><img src="https://688288018-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1flQ2To8tQpCQWl2Ty9U%2Fuploads%2FQy0fD6jqIPLdDeGwC66Q%2Fimage.png?alt=media&#x26;token=8098c96a-219f-4a2e-b004-8c8f71cf545f" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
The value of the id field contains code that generates a random UUID number.
{% endhint %}

Add the remaining field mapping rows for the first name, last name, birth date and social security as shown in the image below.&#x20;

<figure><img src="https://688288018-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1flQ2To8tQpCQWl2Ty9U%2Fuploads%2FlKJBf0Nzgt5lD6ujKVXb%2Fimage.png?alt=media&#x26;token=a277c3f9-7fed-4dbf-b55d-1805665cbc13" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
We are mapping the first name, last name, birth date, and social security number from `input.payload`. The `input.payload` contains information submitted in the body of a request.
{% endhint %}

Navigate back to the **Swagger** page and select the web service endpoint `/rest/add_customer`*.* Click on the **Try it out** button and add a JSON object of customer information as shown in the image below. Click on the **Execute** button.

<figure><img src="https://688288018-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1flQ2To8tQpCQWl2Ty9U%2Fuploads%2FEbH2ryhXMFiwAsqkC96O%2Fimage.png?alt=media&#x26;token=78ec2dbf-63e7-438b-9188-fc9150afcb89" alt=""><figcaption></figcaption></figure>

On the **Run History** page, you would see the integration `add_customer` have the customer information that we submitted in the body of the request within `input.payload`.

<figure><img src="https://688288018-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1flQ2To8tQpCQWl2Ty9U%2Fuploads%2FgWhr06znkxKICBYKT72p%2Fimage.png?alt=media&#x26;token=4ee79308-34b2-4cf5-a10c-e2096741053c" alt=""><figcaption></figcaption></figure>

On the **Run History Item**, click on the `insert_customer` request and you would see in the response the field mappings we added in the previous step. We will use `response.payload` of the service request `insert_customer` to add a customer in the **data** vault.

<figure><img src="https://688288018-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1flQ2To8tQpCQWl2Ty9U%2Fuploads%2Fc22eNezCbIaGgFQLjc2D%2Fimage.png?alt=media&#x26;token=3673fdf8-eb0b-4360-97a1-4aa7a2d00007" alt=""><figcaption></figcaption></figure>

To add a customer in the **data** vault, navigate to the service request `insert_customer` and add the following [code](https://glyue.docs.sandboxbanking.com/tutorials/building-a-restful-crud-web-service/5.-vault-code-examples-and-explanation#add-a-customer) in the **After Execute Request Success Hook**.&#x20;

<figure><img src="https://688288018-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1flQ2To8tQpCQWl2Ty9U%2Fuploads%2FG0oWoAvZ3UuxYQHlWQPD%2Fimage.png?alt=media&#x26;token=84058905-6547-46ce-8d34-c69e24473542" alt=""><figcaption></figcaption></figure>

Navigate back to the **Swagger** page and execute the web service endpoint `/rest/add_customer` again. *Y*ou should receive the id of the customer in the response body.

<figure><img src="https://688288018-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1flQ2To8tQpCQWl2Ty9U%2Fuploads%2FI20lQWNmb71E06Nlb8W2%2Fimage.png?alt=media&#x26;token=907bdcb5-db46-4145-8820-ba05fc42bd69" alt=""><figcaption></figcaption></figure>

Insert another customer as shown in the image below and execute the web service endpoint `/rest/add_customer`.

<figure><img src="https://688288018-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1flQ2To8tQpCQWl2Ty9U%2Fuploads%2FHLH5IWa5XuoHE0Se3B9J%2Fimage.png?alt=media&#x26;token=68fabdbc-0e65-40ab-b84f-f543c6a420ee" alt=""><figcaption></figcaption></figure>

Afterwards, execute the web service endpoint `/rest/get_all_customers`. You should see both customers in the body of the response.

<div align="center"><figure><img src="https://688288018-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1flQ2To8tQpCQWl2Ty9U%2Fuploads%2FkNKGYMUzn3fKco7S13RC%2Fimage.png?alt=media&#x26;token=34d56370-f026-4468-bb2c-997d9ab93cbc" alt="" width="563"><figcaption></figcaption></figure></div>

### Get Customer By Id

Navigate back to the **Integration** table on the **Build** page in Integration Gateway and select the integration `get_customer`. Add the following [code](https://glyue.docs.sandboxbanking.com/tutorials/building-a-restful-crud-web-service/5.-vault-code-examples-and-explanation#get-customer-by-id) in the **Before Hook**.&#x20;

<figure><img src="https://688288018-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1flQ2To8tQpCQWl2Ty9U%2Fuploads%2FOQXzvax99cIZ3GpFap16%2Fimage.png?alt=media&#x26;token=44579a90-05f0-4cf4-ad7e-4b61b8145eff" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
`input.path` is the location where path parameter values can be accessed.
{% endhint %}

Navigate back to the **Swagger** page and select the web service endpoint `/rest/get_customer`. Click on the **Try it out** button and add a valid id path parameter as shown in the image below.

<figure><img src="https://688288018-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1flQ2To8tQpCQWl2Ty9U%2Fuploads%2F82kP7CJTmrMitAbUDM4B%2Fimage.png?alt=media&#x26;token=fa86b8f4-2fbd-46d1-826f-f43280ec0215" alt=""><figcaption></figcaption></figure>

Click on the **Execute** button. *Y*ou should receive the customer's information in the form of a JSON object in the response body.

<figure><img src="https://688288018-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1flQ2To8tQpCQWl2Ty9U%2Fuploads%2F5dEo6EGdtPTIs4wRuzbc%2Fimage.png?alt=media&#x26;token=4f8589c3-2624-4374-97e0-7ea04baaf32a" alt=""><figcaption></figcaption></figure>

### Update Customer

The following steps are similar to what we did to add a customer in the **data** vault. The major difference is we omit the id field because the customer Id should never change.

Create the following service request for the `update_customer` integration.

* **Sequence** - 1
* **System** - ECHO
* **Service Name** - N/A
* **Formula Variable** - modify\_customer

Click on the **Save** button below.&#x20;

<figure><img src="https://688288018-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1flQ2To8tQpCQWl2Ty9U%2Fuploads%2FJlwyTtB3hPIVaM4NtiAp%2Fimage.png?alt=media&#x26;token=dc633fa0-94c2-4cce-8ad5-3866eae8bac2" alt=""><figcaption></figcaption></figure>

Next, create the following field mapping rows for the service request `modify_customer`.&#x20;

<figure><img src="https://688288018-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1flQ2To8tQpCQWl2Ty9U%2Fuploads%2FTFasdJbnCR1ZCWQJTE6Z%2Fimage.png?alt=media&#x26;token=fc491efa-f2ad-4744-b6da-c27b37415d8d" alt=""><figcaption></figcaption></figure>

In the field mapping table, we are using the **Call If** column to omit or include a field and value in `response.payload` based on the existence of the field in `input.payload`.&#x20;

Navigate back to the `modify_customer` service request and add the following [code](https://glyue.docs.sandboxbanking.com/tutorials/building-a-restful-crud-web-service/5.-vault-code-examples-and-explanation#update-customer) in the **After Execute Request Success Hook**.&#x20;

<figure><img src="https://688288018-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1flQ2To8tQpCQWl2Ty9U%2Fuploads%2FIuGODwTCPSWPInKK7FVz%2Fimage.png?alt=media&#x26;token=76f951fc-d45c-4578-a897-19dd28154c93" alt=""><figcaption></figcaption></figure>

Navigate back to the **Swagger** page. Select the web service endpoint `/rest/update_customer` and click on the **Try it out** button. Add a valid id path parameter and a new first name and last name in the body of the request as shown in the image below.&#x20;

<figure><img src="https://688288018-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1flQ2To8tQpCQWl2Ty9U%2Fuploads%2FnkK9GoA6BZrHwTHajEyz%2Fimage.png?alt=media&#x26;token=0518521c-b852-4678-a8e8-23d086b48c32" alt=""><figcaption></figcaption></figure>

Click on the **Execute** button. You should receive the customer's information in the form of a JSON object with the first name and last name updated.&#x20;

<figure><img src="https://688288018-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1flQ2To8tQpCQWl2Ty9U%2Fuploads%2Frm6gmqarrcqIBo8ZWksb%2Fimage.png?alt=media&#x26;token=47e76220-16c6-42ef-9ea7-3c2d74bfcb12" alt=""><figcaption></figcaption></figure>

### Delete Customer

Navigate back to **Integration** table on the **Build** page in Integration Gateway and select the integration `delete_customer`. Add the following [code](https://glyue.docs.sandboxbanking.com/tutorials/building-a-restful-crud-web-service/5.-vault-code-examples-and-explanation#delete-customer) in the **Before Hook**. &#x20;

<figure><img src="https://688288018-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1flQ2To8tQpCQWl2Ty9U%2Fuploads%2FYgI04ls20Kxlp5skFRrP%2Fimage.png?alt=media&#x26;token=49574c27-715b-488f-97d6-2db8c947b596" alt=""><figcaption></figcaption></figure>

Navigate back to the **Swagger** page and select the web service endpoint `/rest/update_customers`. Click on the **Try it out** button and provide a valid id path parameter as shown in the image below.

<figure><img src="https://688288018-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1flQ2To8tQpCQWl2Ty9U%2Fuploads%2FANMBeWVxs9YZPRQ8aK4t%2Fimage.png?alt=media&#x26;token=16a44dba-1158-482e-a37d-825ab3c72f2d" alt=""><figcaption></figcaption></figure>

You should receive a confirmation message that the customer was deleted.

<figure><img src="https://688288018-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1flQ2To8tQpCQWl2Ty9U%2Fuploads%2FaHaVnGqEiSMammK74Jfc%2Fimage.png?alt=media&#x26;token=0dd78244-adf5-40ae-b9c8-0120bf56647e" alt=""><figcaption></figcaption></figure>
