Answers.Body: <p>So there are two things wrong with your code snippet.</p>
  
  <p>First off, the <code>fetch()</code> method takes four parameters in the following order: <code>client</code>, <code>method</code>, <code>destination</code>, and <code>options</code>. In your example above, you're passing your options object in as the <code>destination</code> (because that is the third parameter), which is not going to work.</p>
  
  <p>So you'll want to do something like the following instead:</p>
  
  <pre><code>{% set client = {
      base_uri : 'https://api.tickets.com',
      timeout : 10
  } %}
  
  {% set destination = 'rest/v2/Events/2704/Reviews?order=1&amp;count=30' %}
  
  {% set options = {
      headers: {
          'Content-Type': 'application/json',
          'apiKey': 'aaabbbcccdddeee',
      }
  } %}
  
  {% set request = fetch(client, 'GET', destination, options) %}
  </code></pre>
  
  <p>We've made the <code>client.base_uri</code> actually the Base URI (by removing the segments and query strings) and moved those to a separate variable (which is <em>technically</em> where they belong). This allows us to now have four parameters that we're passing into <code>fetch()</code> in the right order (notice our <code>options</code> object is now the fourth parameter like it should be).</p>
  
  <p>In addition, it should be <code>headers: {</code>, not <code>headers: new Headers({</code> in your options hash. <code>headers: new Headers({</code> is not valid Twig. Not sure where you got the <code>new Headers()</code> from but a simple hash should work well (similar to <a href="http://docs.guzzlephp.org/en/stable/request-options.html#headers" rel="noreferrer">the PHP example</a> in the Guzzle docs).</p>
  
  <hr>
  
  <p>Your second issue (and this is the bigger one) is this:</p>
  
  <pre><code>{% echo $request-&gt;getReasonPhrase(); %}
  </code></pre>
  
  <p>That is not proper Twig at all. Looks like you grabbed a bit of PHP code from somewhere and popped it inside <code>{% ... %}</code>, which is <strong><em>never</em></strong> going to work.</p>
  
  <p>If you take a look at the <a href="https://github.com/lukeyouell/craft-fetch" rel="noreferrer">Fetch README</a>, it shows you what a response is going to look like: </p>
  
  <pre><code>{
      "statusCode":200,
      "reason":"OK",
      "body": {
          "credits_left":30,
          "test_id":"JDHFbrt7",
          "poll_state_url":"https:\/\/gtmetrix.com\/api\/0.1\/test\/JDHFbrt7"
      }
  }
  </code></pre>
  
  <p>What this snippet means is after you run <code>{% set request = fetch(...) %}</code>, the <code>request</code> variable is now going to equal a Twig object (more properly <a href="https://mijingo.com/blog/key-value-arrays-in-twig" rel="noreferrer">known as a <code>hash</code></a>) containing the status code of the response, the reason phrase (which is basically just a <a href="https://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html" rel="noreferrer">description of a status code</a>; so the corresponding reason phrase for a status code of <code>200</code> is <code>OK</code>, for a <code>404</code> it's going to be <code>Not Found</code>, etc.), and then finally the body of the response returned from the API endpoint.</p>
  
  <p>You'd access these variables no differently than you would any other hash in Twig. To get the status code and/or the reason phrase, do:</p>
  
  <pre><code>{{ request.statusCode }} - {{ request.reason }}
  </code></pre>
  
  <p>To access any property of the request body, you would do:</p>
  
  <pre><code>{{ request.body.myProperty }}
  </code></pre>
  
  <p>Depending on the structure of the API response, you might have to dig down into several levels of nested hashes: </p>
  
  <pre><code>{{ request.body.oneHash.subHash.somethingElse.myProperty }}
  </code></pre>
  
  <p>You get the idea...</p>
  
  <p>If you're not sure what the API response looks like (often times the API you're using will show you an example of what the response body will look like, so I'd look at the API docs first), you can just <code>dump()</code> the whole body property to see what it contains:</p>
  
  <pre><code>{{ dump(request.body) }}
  </code></pre>
  
  <hr>
  
  <p>Hopefully that helps!</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-26T15:41:14.167Z
Answers.Text: Amazing Response! Make a pull request for Fetch and add this in.
Answers.UserDisplayName: JMKelley
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-26T15:36:52.577Z
Answers.LastActivityDate: 2019-02-26T20:51:21.950Z
Answers.LastEditDate: 2019-02-26T20:51:21.950Z
Answers.UserDisplayName: Jalen Davenport
Body: <p>I'm currently in the process of calling an API endpoint and trying to display the data relating to it. But I'm having a bit of trouble using 'Guzzle/Fetch' in this scenario. I have the following code but it seems to be outputting nothing at all. (Plugin is all installed)</p>
  
  <p><strong>Template.html</strong></p>
  
  <pre><code>{% set client = {
      base_uri : 'https://api.tickets.com/rest/v2/Events/2704/Reviews?order=1&amp;count=30',
      timeout : 10
  } %}
  
  {% set options = {
      headers: new Headers({
        'Content-Type': 'application/json',
        'apiKey': 'aaabbbcccdddeee',
      })
  } %}
  
  {% set request = fetch(client, 'GET', options) %}
  
  {% echo $request-&gt;getReasonPhrase(); %}
  </code></pre>
  
Tags: <templating>
Title: Fetching & Displaying an API Request using 'Fetch'
ThreadText: # Fetching & Displaying an API Request using 'Fetch'
  
  (Asked by: username_0 on 2019-02-25)
  
  I'm currently in the process of calling an API endpoint and trying to display the data relating to it. But I'm having a bit of trouble using 'Guzzle/Fetch' in this scenario. I have the following code but it seems to be outputting nothing at all. (Plugin is all installed)
  
  **Template.html**
  
  ```
  {% set client = {
      base_uri : 'https://api.tickets.com/rest/v2/Events/2704/Reviews?order=1&count=30',
      timeout : 10
  } %}
  
  {% set options = {
      headers: new Headers({
        'Content-Type': 'application/json',
        'apiKey': 'aaabbbcccdddeee',
      })
  } %}
  
  {% set request = fetch(client, 'GET', options) %}
  
  {% echo $request->getReasonPhrase(); %}
  
  ```
  
  **Tags:** templating
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-02-26. Score: 7 | Accepted answer
  
  So there are two things wrong with your code snippet.
  
  First off, the `fetch()` method takes four parameters in the following order: `client`, `method`, `destination`, and `options`. In your example above, you're passing your options object in as the `destination` (because that is the third parameter), which is not going to work.
  
  So you'll want to do something like the following instead:
  
  ```
  {% set client = {
      base_uri : 'https://api.tickets.com',
      timeout : 10
  } %}
  
  {% set destination = 'rest/v2/Events/2704/Reviews?order=1&count=30' %}
  
  {% set options = {
      headers: {
          'Content-Type': 'application/json',
          'apiKey': 'aaabbbcccdddeee',
      }
  } %}
  
  {% set request = fetch(client, 'GET', destination, options) %}
  
  ```
  
  We've made the `client.base_uri` actually the Base URI (by removing the segments and query strings) and moved those to a separate variable (which is *technically* where they belong). This allows us to now have four parameters that we're passing into `fetch()` in the right order (notice our `options` object is now the fourth parameter like it should be).
  
  In addition, it should be `headers: {`, not `headers: new Headers({` in your options hash. `headers: new Headers({` is not valid Twig. Not sure where you got the `new Headers()` from but a simple hash should work well (similar to [the PHP example](http://docs.guzzlephp.org/en/stable/request-options.html#headers) in the Guzzle docs).
  
  ---
  
  Your second issue (and this is the bigger one) is this:
  
  ```
  {% echo $request->getReasonPhrase(); %}
  
  ```
  
  That is not proper Twig at all. Looks like you grabbed a bit of PHP code from somewhere and popped it inside `{% ... %}`, which is ***never*** going to work.
  
  If you take a look at the [Fetch README](https://github.com/lukeyouell/craft-fetch), it shows you what a response is going to look like:
  
  ```
  {
      "statusCode":200,
      "reason":"OK",
      "body": {
          "credits_left":30,
          "test_id":"JDHFbrt7",
          "poll_state_url":"https:\/\/gtmetrix.com\/api\/0.1\/test\/JDHFbrt7"
      }
  }
  
  ```
  
  What this snippet means is after you run `{% set request = fetch(...) %}`, the `request` variable is now going to equal a Twig object (more properly [known as a `hash`](https://mijingo.com/blog/key-value-arrays-in-twig)) containing the status code of the response, the reason phrase (which is basically just a [description of a status code](https://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html); so the corresponding reason phrase for a status code of `200` is `OK`, for a `404` it's going to be `Not Found`, etc.), and then finally the body of the response returned from the API endpoint.
  
  You'd access these variables no differently than you would any other hash in Twig. To get the status code and/or the reason phrase, do:
  
  ```
  {{ request.statusCode }} - {{ request.reason }}
  
  ```
  
  To access any property of the request body, you would do:
  
  ```
  {{ request.body.myProperty }}
  
  ```
  
  Depending on the structure of the API response, you might have to dig down into several levels of nested hashes:
  
  ```
  {{ request.body.oneHash.subHash.somethingElse.myProperty }}
  
  ```
  
  You get the idea...
  
  If you're not sure what the API response looks like (often times the API you're using will show you an example of what the response body will look like, so I'd look at the API docs first), you can just `dump()` the whole body property to see what it contains:
  
  ```
  {{ dump(request.body) }}
  
  ```
  
  ---
  
  Hopefully that helps!
  
  > **Comments:**
  > **username_0**: Amazing Response! Make a pull request for Fetch and add this in.  
  
  ---
  
  