title: strapi FetchError: request to http://localhost:1337/api/events failed, reason: connect ECONNREFUSED ::1:1337
body: <p>If I use my browser to access localhost:1337/api/events as a PUBLIC (Unauthenticated user) I get the following returned:</p>
  <pre><code>{&quot;data&quot;:[{&quot;id&quot;:1,&quot;attributes&quot;:{&quot;name&quot;:&quot;Throwback Thursday with DJ Manny Duke&quot;,&quot;slug&quot;:&quot;throwback-thursday-with-dj-manny-duke&quot;,&quot;venue&quot;:&quot;Horizon Club&quot;,&quot;address&quot;:&quot;919 3rd Ave New York, New York(NY), 1002&quot;,&quot;date&quot;:&quot;2022-07-20T02:00:00.000Z&quot;,&quot;time&quot;:&quot;10:00 PM&quot;,&quot;createdAt&quot;:&quot;2022-04-12T02:05:08.246Z&quot;,&quot;updatedAt&quot;:&quot;2022-04-12T02:17:16.760Z&quot;,&quot;publishedAt&quot;:&quot;2022-04-12T02:05:16.192Z&quot;,&quot;performers&quot;:&quot;DJ Manny Duke&quot;,&quot;description&quot;:&quot;Description for the vent of DJ Manny Duke&quot;}},{&quot;id&quot;:2,&quot;attributes&quot;:{&quot;name&quot;:&quot;Boom Dance Festival Experience&quot;,&quot;slug&quot;:&quot;boom-dance-festival-experience&quot;,&quot;venue&quot;:&quot;Blackjacks&quot;,&quot;address&quot;:&quot;123 Lexington&quot;,&quot;date&quot;:&quot;2022-04-25T16:00:00.000Z&quot;,&quot;time&quot;:&quot;8:00 PM&quot;,&quot;createdAt&quot;:&quot;2022-04-12T02:26:32.123Z&quot;,&quot;updatedAt&quot;:&quot;2022-04-12T02:26:33.540Z&quot;,&quot;publishedAt&quot;:&quot;2022-04-12T02:26:33.538Z&quot;,&quot;performers&quot;:&quot;DJ LUKE, DJ BLACKJACK&quot;,&quot;description&quot;:&quot;Whatever Description&quot;}},{&quot;id&quot;:3,&quot;attributes&quot;:{&quot;name&quot;:&quot;Encore Night Boat Party&quot;,&quot;slug&quot;:&quot;encore-night-boat-party&quot;,&quot;venue&quot;:&quot;Encore&quot;,&quot;address&quot;:&quot;12343 New York&quot;,&quot;date&quot;:&quot;2022-11-14T16:00:00.000Z&quot;,&quot;time&quot;:&quot;7:00 PM&quot;,&quot;createdAt&quot;:&quot;2022-04-12T02:28:06.028Z&quot;,&quot;updatedAt&quot;:&quot;2022-04-12T02:28:36.292Z&quot;,&quot;publishedAt&quot;:&quot;2022-04-12T02:28:07.622Z&quot;,&quot;performers&quot;:&quot;BAD BOY BILL&quot;,&quot;description&quot;:&quot;Description of Encore&quot;}}],&quot;meta&quot;:{&quot;pagination&quot;:{&quot;page&quot;:1,&quot;pageSize&quot;:25,&quot;pageCount&quot;:1,&quot;total&quot;:3}}}
  </code></pre>
  <p>However when I use Next.JS to access the same link I get:</p>
  <pre><code>FetchError: request to http://localhost:1337/api/events failed, reason: connect ECONNREFUSED ::1:1337
  </code></pre>
  <p>Why is strapi refusing connection? How to fix?</p>
  <p>config/index.js</p>
  <pre><code>export const API_URL =
      process.env.NEXT_PUBLIC_API_URL || 'http://localhost:1337'
  </code></pre>
  <p>pages/index.js</p>
  <p>...</p>
  <pre><code>export async function getStaticProps() {
          const res = await fetch(`${API_URL}/api/events`)
          const events = await res.json()
      
          return {
              props: { events: events.slice(0, 3) },
              revalidate: 1,
          }
      }
  </code></pre>
  <p>-----UPDATED CODE TO BELOW BUT STILL REFUSING CONNECTION ----</p>
  <p>config/index.js
  export const API_URL = 'http://localhost:1337'</p>
  <p>pages/index.js</p>
  <pre><code>export async function getStaticProps() {
      const res = await fetch(`${API_URL}/api/events`, {
          headers: {
              Authorization: `bearer thetoken`,
          },
      })
      const events = await res.json()
  
      return {
          props: { events: events.slice(0, 3) },
          revalidate: 1,
      }
  }
  </code></pre>
  <p>----BELOW IS SCREENSHOT OF ERROR AND THE 2 CONSOLES SHOWING THE CLIENT (NEXT.JS) AND SERVER (STRAPI) ARE BOTH RUNNING----</p>
  <p><a href="https://i.stack.imgur.com/dBvow.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dBvow.jpg" alt="enter image description here" /></a></p>