title: Cypress : save data from API response to re-use it
body: <p>I try to use Cypress to test my application. In this application I have a form ; submitting this s from cause a POST API call. The API response contains the ID of the created resource. I ould like to save this ID into a variable to reuse it later in my test (to clean the resource at the end of the test).</p>
  <p>This is my cypress code</p>
  <pre><code>
  describe('Templates', function() {
  
    let templateID = null
  
    before('before once', function() {
        cy.server();
        // define a route with alias for my POST requesst
        cy.route({method: 'POST', url:'/api/templates/'}).as('api_template_create')
        cy.visit('');
    });
  
    it('Create a new template', function() {
      cy.get('.my-form #type').select('book')
      cy.get('.my-form #title').type('Cyress title test')
      ...
      cy.get('.my-form input:submit').click()
      cy.wait('@api_template_create').then(xhr =&gt; {
        const response = xhr.response.body
        templateID = response.metadata.pid
        cy.log(templateID) // at this time 'templateID' contains the correct value
      })
      cy.wait(2000)
      cy.log(templateID) // templateID is null (as initial value)
  
      // continue test....
      // ...at the end call the DELETE API with templateID as param
    });
  });
  
  </code></pre>
  <p>See below the result of this code :</p>
  <p><a href="https://i.stack.imgur.com/yfaDo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yfaDo.png" alt="cypress test result" /></a></p>
  <p>L#23 is the log into the wait().then() --&gt; correct one \<br />
  L#25 is the log after in the main 'it' test --&gt; return to null (as initial value)</p>
  <p>Is it a way to solve my problem ?</p>