Accessing Data Across Steps

A tale as old as time: an ID returned by a request in step 1 must be passed into request in step 2. We can make use of Coast's Local Database and Dynamic Default Value to achieve this experience.

Example

Let's assume we have an endpoint where a user logs in with their credentials, and the response contains a token, which we will need later on in the demo, to execute a subsequent request. The goal is to be able to access the token returned in step 1's response, and feed this token into a request in step 2.

  1. Begin by adding a new step, with a request and response tab. This step will handle the user sign in. Fill in the request with the following details:
POST
Dummy Login Request
curl --request POST \
	--url https://dummyjson.com/auth/login \
	--header 'Content-Type: application/json' \
	--data '
{
  "username": "kminchelle",
  "password": "0lelplR"
}
'
  1. Inspect the request's tab DB Key by pressing the ⚙️ icon. Let's change it to something more descriptive: userLogin

The DB Key is where all the data relating to this request will be stored in Coast's Local Database.

  1. Populate the response tab with the following payload & ensure that it is also set to live, and linked to Request (1)
JSON
Dummy Login Response
{
  "id": 15,
  "username": "kminchelle",
  "email": "kminchelle@qq.com",
  "firstName": "Jeanne",
  "lastName": "Halvorson",
  "gender": "female",
  "image": "https://robohash.org/autquiaut.png",
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MTUsInVzZXJuYW1lIjoia21pbmNoZWxsZSIsImVtYWlsIjoia21pbmNoZWxsZUBxcS5jb20iLCJmaXJzdE5hbWUiOiJKZWFubmUiLCJsYXN0TmFtZSI6IkhhbHZvcnNvbiIsImdlbmRlciI6ImZlbWFsZSIsImltYWdlIjoiaHR0cHM6Ly9yb2JvaGFzaC5vcmcvYXV0cXVpYXV0LnBuZyIsImlhdCI6MTY5NTI1NDQzMiwiZXhwIjoxNjk1MjU4MDMyfQ.VeV80AKtj4kXVYHizN12PD8CNHsbdFJZx89RtN-ADZs"
}
  1. Let's now add a new step, where we will execute a transaction (mocked), which requires the user's token. Add a new step, with a Request tab and populate it with the following details:
POST
Dummy Login Request
curl --request POST \
	--url https://exampletransaction/ \
	--data '
{
  "amount": 100,
  "token": "",
  "productId": "3493453223"
}
'
  1. While in the Editor, tap on Format, and then select the token field. Now, in the Dynamic Default Value code editor, tap on State Value and search for "token". This will query Coast's Local Database's keys and values. Tap on token to see it's path get populated in the code editor.

Alternatively, you can simply paste in the following path to the code editor: this.db.userLogin.response.token.

Now, the token value on this request will dynamically read in the token returned from the userLogin tab 🚀

  1. (Optional) Flip over to the designer and select some gallery templates for the login Request, Response and Transaction tabs to see this in action.

    Boom!