# CORS error builder component from @builder.io/react in localhost

**URL:** https://forum.builder.io/t/cors-error-builder-component-from-builder-io-react-in-localhost/4836
**Category:** Technical Questions
**Created:** [January 22, 2024, 4:19pm UTC](https://forum.builder.io/t/cors-error-builder-component-from-builder-io-react-in-localhost/4836 "2024-01-22T16:19:51Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Luiz](https://avatars.discourse-cdn.com/v4/letter/l/f04885/32.png) [@Luiz](https://forum.builder.io/u/Luiz)
#### Post date: [January 22, 2024, 4:19pm UTC](https://forum.builder.io/t/cors-error-builder-component-from-builder-io-react-in-localhost/4836/1 "2024-01-22T16:19:51Z")

</div>

builder component from @builder.io/react doesnt’work when I try to make a request, It’s returning CORS errors. It’s working just when I use axios.

example code:

const getArticles = async () =\> {  
try {  
const response = await axios.get(`https://cdn.builder.io/api/v3/content/page-article?apiKey=${process.env.NEXT_PUBLIC_BUILDER_API_KEY}&query.data.title.$options=i&&query.data.title.$regex=.*${searchTerm}.*&limit=20&omit=data.blocks`);

```
    setSearchResults(response);
  } catch (error) {
    console.error('Error:', error);
    throw error;
  }
};

```

---

<div class="post-metadata">

### Author: ![manish-sharma](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@manish-sharma](https://forum.builder.io/u/manish-sharma)
#### Post date: [January 22, 2024, 5:55pm UTC](https://forum.builder.io/t/cors-error-builder-component-from-builder-io-react-in-localhost/4836/2 "2024-01-22T17:55:12Z")

</div>

Hello @Luiz,

Are you getting the CORS error when using axios?

---

<div class="post-metadata">

### Author: ![Luiz](https://avatars.discourse-cdn.com/v4/letter/l/f04885/32.png) [@Luiz](https://forum.builder.io/u/Luiz)
#### Post date: [January 23, 2024, 10:31am UTC](https://forum.builder.io/t/cors-error-builder-component-from-builder-io-react-in-localhost/4836/3 "2024-01-23T10:31:30Z")

</div>

No, Actually when I’m using the builder component, for example:

import { builder } from ‘@builder.io/react’;

builder.get(`https://cdn.builder.io/api/v3/content/pible-page-article?apiKey=${process.env.NEXT_PUBLIC_BUILDER_API_KEY}&query.data.title.$options=i&&query.data.title.$regex=.*${searchTerm}.*&limit=20&omit=data.blocks`)

---

<div class="post-metadata">

### Author: ![manish-sharma](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@manish-sharma](https://forum.builder.io/u/manish-sharma)
#### Post date: [January 23, 2024, 10:51am UTC](https://forum.builder.io/t/cors-error-builder-component-from-builder-io-react-in-localhost/4836/4 "2024-01-23T10:51:11Z")

</div>

Hello @Luiz!

Could you share your complete code?

---

<div class="post-metadata">

### Author: ![Luiz](https://avatars.discourse-cdn.com/v4/letter/l/f04885/32.png) [@Luiz](https://forum.builder.io/u/Luiz)
#### Post date: [January 23, 2024, 1:11pm UTC](https://forum.builder.io/t/cors-error-builder-component-from-builder-io-react-in-localhost/4836/5 "2024-01-23T13:11:47Z")

</div>

useEffect(() =\> {  
const getArticles = async () =\> {  
try {  
const response = await axios.get(`https://cdn.builder.io/api/v3/content/pible-page-article?apiKey=${process.env.NEXT_PUBLIC_BUILDER_API_KEY}&query.data.title.$options=i&&query.data.title.$regex=.*${searchTerm}.*&limit=20&omit=data.blocks`);

```
    setSearchResults(response.data.results);
  } catch (error) {
    console.error('Error:', error);
    throw error;
  }
};

```

}, [searchTerm]);

using axios it’s ok but if use import { builder } from ‘@builder.io/react’; so I receive CORS error

the only diference is instead of use axios.get(‘…’) I trying to use builder.get(‘…’)

---

<div class="post-metadata">

### Author: ![manish-sharma](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@manish-sharma](https://forum.builder.io/u/manish-sharma)
#### Post date: [January 23, 2024, 1:19pm UTC](https://forum.builder.io/t/cors-error-builder-component-from-builder-io-react-in-localhost/4836/6 "2024-01-23T13:19:40Z")

</div>

Hello @Luiz,

When using the builder SDK, your code would look like the below

```auto
  useEffect(() => {
    builder
      .get("page-article", {
      limit: 100,
      query: {
        'data': { 
          "$or": [ 
            { 
              "title": { "$eq": "Qwik Blog" }
            }, 
            { 
              "title": { "$eq": "Partytown blog" }
            }
          ]
        }
      } 
      })
      .toPromise()
      .then((announcementBar) => setAnnouncement(announcementBar));
  }, []);

```

You should not be using the endpoint like the following `https://cdn.builder.io/api/v3/content/pible-page-article?apiKey=${process.env.NEXT_PUBLIC_BUILDER_API_KEY}&query.data.title.$options=i&&query.data.title.$regex=.*${searchTerm}.*&limit=20&omit=data.blocks)`

> [@REST API query $or doesnt seem to properly work](https://forum.builder.io/t/rest-api-query-or-doesnt-seem-to-properly-work/4835/4):
>
> Hello @djkenneth, Here is an example that is working for us const articles = await builder.getAll('blog-article', { // Include references, like the `author` ref options: { includeRefs: true }, limit: articlesPerPage, query: { 'data': { "$or": [{ "title": { "$eq": "Qwik Blog" } }, { "title": { "$eq": "Partytown blog" } }] } } })

> [@Fulltext search in custom fields](https://forum.builder.io/t/fulltext-search-in-custom-fields/157/2):
>
> Hey @aubergine - try leaving out the \*'s, e.g. query.data.myCustomField.$regex="searchText" E.g. you can query all of our docs that have the text “Builder” in them with this [https://cdn.builder.io/api/v2/content/docs-content?apiKey=YJIGb4i01jvw0SRdL5Bt&query.data.title.$regex="Builder"&fields=name](https://cdn.builder.io/api/v2/content/docs-content?apiKey=YJIGb4i01jvw0SRdL5Bt&query.data.title.%24regex=%22Builder%22&fields=name) You can make this a little nicer too by making the search case insensitive by also adding query.data.myCustomField.$options=i [https://cdn.builder.io/api/v2/content/docs-content?apiKey=YJIGb4i01jvw…](https://cdn.builder.io/api/v2/content/docs-content?apiKey=YJIGb4i01jvw0SRdL5Bt&query.data.title.%24regex=%22Builder%22&fields=name&query.data.title.%24options=i)

---

<div class="post-metadata">

### Author: ![Luiz](https://avatars.discourse-cdn.com/v4/letter/l/f04885/32.png) [@Luiz](https://forum.builder.io/u/Luiz)
#### Post date: [January 23, 2024, 1:28pm UTC](https://forum.builder.io/t/cors-error-builder-component-from-builder-io-react-in-localhost/4836/7 "2024-01-23T13:28:30Z")

</div>

In the first moment I tryed to use this way, exactly like your example @manish-sharma but I always received CORS error in localhost so I tried to use cdn link with axios to see if it will work.

---

<div class="post-metadata">

### Author: ![manish-sharma](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@manish-sharma](https://forum.builder.io/u/manish-sharma)
#### Post date: [January 23, 2024, 1:34pm UTC](https://forum.builder.io/t/cors-error-builder-component-from-builder-io-react-in-localhost/4836/8 "2024-01-23T13:34:12Z")

</div>

Hello @Luiz!

Are you not able to get the data from the SDK call? Would you be able to share a screen recording may be?

---

<div class="post-metadata">

### Author: ![Luiz](https://avatars.discourse-cdn.com/v4/letter/l/f04885/32.png) [@Luiz](https://forum.builder.io/u/Luiz)
#### Post date: [January 23, 2024, 1:48pm UTC](https://forum.builder.io/t/cors-error-builder-component-from-builder-io-react-in-localhost/4836/9 "2024-01-23T13:48:13Z")

</div>

![image](https://us1.discourse-cdn.com/flex020/uploads/builder/original/2X/2/2c217375faada9dfa8c32200b0b362977fa562b1.jpeg)

---

<div class="post-metadata">

### Author: ![manish-sharma](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@manish-sharma](https://forum.builder.io/u/manish-sharma)
#### Post date: [January 23, 2024, 2:03pm UTC](https://forum.builder.io/t/cors-error-builder-component-from-builder-io-react-in-localhost/4836/10 "2024-01-23T14:03:49Z")

</div>

Hello @Luiz,

Can you make sure, you are using the correct API key and call to fetch the data, I tried below and had no issue

```auto
   useEffect(() => {
     
    async function fetchContent() {
      const content = await builder
        .get("pible-page-article", {
          url: window.location.pathname
        })
        .promise();

      setContent(content);
      setNotFound(!content);

      // if the page title is found, 
      // set the document title
      if (content?.data.title) {
       document.title = content.data.title
      }
    }
    fetchContent();
  }, [window.location.pathname]);

```

Additionally, are you using next js or react?

---

<div class="post-metadata">

### Author: ![Luiz](https://avatars.discourse-cdn.com/v4/letter/l/f04885/32.png) [@Luiz](https://forum.builder.io/u/Luiz)
#### Post date: [January 23, 2024, 2:23pm UTC](https://forum.builder.io/t/cors-error-builder-component-from-builder-io-react-in-localhost/4836/11 "2024-01-23T14:23:47Z")

</div>

Yes, I’m using the right api key, Did you try in the local environment?

I’m using next js

---

<div class="post-metadata">

### Author: ![manish-sharma](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@manish-sharma](https://forum.builder.io/u/manish-sharma)
#### Post date: [January 23, 2024, 2:28pm UTC](https://forum.builder.io/t/cors-error-builder-component-from-builder-io-react-in-localhost/4836/12 "2024-01-23T14:28:54Z")

</div>

Hello @Luiz,

Yes, I did try using localhost and if you are using next js you can try below

```auto
import { builder, BuilderComponent } from '@builder.io/react'

builder.init('c62b6400b6304daabf5f25c1107e9986')

export const getStaticProps = async (context) => {
  // Dynamically fetch latest content from Builder.io API
  const content = await builder.get('pible-page-article', { url: context.resolvedUrl });
  return { props: { content } }
}

// View full integration and docs: https://builder.io/c/docs/developers
export default function MyComponent(props) {
  return <BuilderComponent content={props.content} model="pible-page-article" />
}

```

> **[Developer Quickstart - Builder.io](https://www.builder.io/c/docs/quickstart)**
>
> Quickly integrate Builder.io with your application for streamlined Page building with this developer Quickstart.

---

<div class="post-metadata">

### Author: ![Luiz](https://avatars.discourse-cdn.com/v4/letter/l/f04885/32.png) [@Luiz](https://forum.builder.io/u/Luiz)
#### Post date: [January 23, 2024, 2:40pm UTC](https://forum.builder.io/t/cors-error-builder-component-from-builder-io-react-in-localhost/4836/13 "2024-01-23T14:40:44Z")

</div>

But in my page? because in the component it doesn’t work

---

<div class="post-metadata">

### Author: ![manish-sharma](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@manish-sharma](https://forum.builder.io/u/manish-sharma)
#### Post date: [January 23, 2024, 3:27pm UTC](https://forum.builder.io/t/cors-error-builder-component-from-builder-io-react-in-localhost/4836/14 "2024-01-23T15:27:06Z")

</div>

Hello @Luiz,

Can you try the below method

> **[Registering Custom Components - Builder.io](https://www.builder.io/c/docs/custom-components-setup#fetching-data-async)**
>
> Drag and drop with your components, right within your existing site or app. Build and optimize digital experiences for any tech stack, visually.

Additionally, try using CORS plugins [https://chromewebstore.google.com/detail/allow-cors-access-control/lhobafahddgcelffkeicbaginigeejlf](https://chromewebstore.google.com/detail/allow-cors-access-control/lhobafahddgcelffkeicbaginigeejlf)

---

<div class="post-metadata">

### Author: ![Luiz](https://avatars.discourse-cdn.com/v4/letter/l/f04885/32.png) [@Luiz](https://forum.builder.io/u/Luiz)
#### Post date: [January 23, 2024, 3:53pm UTC](https://forum.builder.io/t/cors-error-builder-component-from-builder-io-react-in-localhost/4836/15 "2024-01-23T15:53:23Z")

</div>

My Component is already registered and this extension didn’t work ☹

---

<div class="post-metadata">

### Author: ![Luiz](https://avatars.discourse-cdn.com/v4/letter/l/f04885/32.png) [@Luiz](https://forum.builder.io/u/Luiz)
#### Post date: [January 23, 2024, 5:12pm UTC](https://forum.builder.io/t/cors-error-builder-component-from-builder-io-react-in-localhost/4836/16 "2024-01-23T17:12:13Z")

</div>

I need to make a request to search in client side, I need to fetch some articles using a search input for example

---

<div class="post-metadata">

### Author: ![Luiz](https://avatars.discourse-cdn.com/v4/letter/l/f04885/32.png) [@Luiz](https://forum.builder.io/u/Luiz)
#### Post date: [January 25, 2024, 11:01am UTC](https://forum.builder.io/t/cors-error-builder-component-from-builder-io-react-in-localhost/4836/17 "2024-01-25T11:01:33Z")

</div>

My Component is already registered and this extension didn’t work

I need to make a request to search in client side, I need to fetch some articles using a search input for example

---

<div class="post-metadata">

### Author: ![manish-sharma](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@manish-sharma](https://forum.builder.io/u/manish-sharma)
#### Post date: [January 25, 2024, 11:03am UTC](https://forum.builder.io/t/cors-error-builder-component-from-builder-io-react-in-localhost/4836/18 "2024-01-25T11:03:16Z")

</div>

Hello @Luiz!

Please help with your complete component code, so that we can reproduce the issue and assist you further.

Thanks,

---

<div class="post-metadata">

### Author: ![Luiz](https://avatars.discourse-cdn.com/v4/letter/l/f04885/32.png) [@Luiz](https://forum.builder.io/u/Luiz)
#### Post date: [January 25, 2024, 12:15pm UTC](https://forum.builder.io/t/cors-error-builder-component-from-builder-io-react-in-localhost/4836/20 "2024-01-25T12:15:00Z")

</div>

import React, { useState, useEffect } from ‘react’;  
import { Container, Input, SearchIconSection } from ‘./styles’;  
import { FontAwesomeIcon } from ‘@fortawesome/react-fontawesome’;  
import { faSearch } from ‘@fortawesome/free-solid-svg-icons’;  
import { builder } from ‘@builder.io/react’;  
import { useRouter } from ‘next/router’;  
// import axios from ‘axios’;

type Props = {  
placeholder: string;  
};

builder.init(process.env.NEXT\_PUBLIC\_BUILDER\_API\_KEY || ‘’);

function InputSearch(props: Props) {  
const router = useRouter();

const [searchTerm, setSearchTerm] = useState(‘’);  
const [searchResults, setSearchResults] = useState();

/\* useEffect(() =\> {  
const getArticles = async () =\> {  
try {  
const response = await axios.get(`https://cdn.builder.io/api/v3/content/pible-page-article?apiKey=${process.env.NEXT_PUBLIC_BUILDER_API_KEY}&query.data.title.$options=i&&query.data.title.$regex=.*${searchTerm}.*&limit=20&omit=data.blocks`);

```
    setSearchResults(response.data.results);
    return response.data.results;
  } catch (error) {
    console.error('Error:', error);
    throw error;
  }
};

const delaySearch = setTimeout(() => {
  if (searchTerm.trim() !== '') {
    getArticles(searchTerm);
  } else {
    // setSuggestions([]);
  }
}, 300);

return () => clearTimeout(delaySearch);

```

}, [searchTerm]); \*/

useEffect(() =\> {  
if (searchTerm) {  
builder  
.get(‘page-article’, {  
limit: 100,  
query: {  
data: {  
$or: [  
{  
title: { $eq: ‘dev’ }, // just for test  
},  
],  
},  
},  
})  
.toPromise()  
.then((response) =\> setSearchResults(response));  
}  
}, [searchTerm]);

const handleSearch = () =\> {  
router.push(`/tax-pible/${searchTerm}`);  
};

return (  
  
\<Input  
type=“text”  
placeholder={props.placeholder}  
value={searchTerm}  
onChange={(e) =\> setSearchTerm(e.target.value)}  
/\>

```
  <SearchIconSection onClick={handleSearch}>
    <FontAwesomeIcon icon={faSearch} />
  </SearchIconSection>

  {/* <ul>
    {searchResults.length > 0 && searchResults.map(({ data }, index) => {
      console.log('ields ', data.title)
      return (
        <li key={index}>{data.title}</li>
      )
    })}
  </ul> */}
</Container>

```

);  
}

export default InputSearch;

---

<div class="post-metadata">

### Author: ![manish-sharma](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@manish-sharma](https://forum.builder.io/u/manish-sharma)
#### Post date: [January 25, 2024, 2:42pm UTC](https://forum.builder.io/t/cors-error-builder-component-from-builder-io-react-in-localhost/4836/21 "2024-01-25T14:42:55Z")

</div>

Hello @Luiz,

I’m not able to reproduce the issue on my end, please refer to the loom recording below

[Next page](https://forum.builder.io/t/cors-error-builder-component-from-builder-io-react-in-localhost/4836.md?page=2)
