# I want to send data through an API in a UI Extension using React.

**URL:** <https://community.hubspot.com/t/i-want-to-send-data-through-an-api-in-a-ui-extension-using-react/135387>\
**Category:** CMS Development\
**Tags:** free\
**Created:** [June 30, 2025, 6:56am UTC](https://community.hubspot.com/t/i-want-to-send-data-through-an-api-in-a-ui-extension-using-react/135387 "2025-06-30T06:56:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![SagarTomar](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@SagarTomar](https://community.hubspot.com/u/SagarTomar)\
**Post date:** [June 30, 2025, 6:56am UTC](https://community.hubspot.com/t/i-want-to-send-data-through-an-api-in-a-ui-extension-using-react/135387/1 "2025-06-30T06:56:07Z")

</div>

Thank you in advance

I want to send data through an API in a UI Extension using React.i am facing this error

TypeError: i is not a function

at t.U (e263900d-d68d-4be7-a210-d19fc04831d6.js?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20250630T063335Z&X-Amz-SignedHeaders=host&X-Amz-Expires=28800&X-Amz-Credential=AKIAWQDZYH6KDTJIVD6M%2F20250630%2Fus-east-1%2Fs3%2Faws4\_request&X-Amz-Signature=340384d23c801723b87e3fb7029c3cbde988dbd35143349cb521ce1e5b667594:1:4711)

at t (index.js:403:30)

at Object.call (index.js:102:30)

at MessagePort.d (index.js:408:42) why i am facing this error

---

<div class="post-metadata">

**Author:** ![GiantFocal](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/giantfocal/32/41969_2.png) [@GiantFocal](https://community.hubspot.com/u/GiantFocal)\
**Post date:** [June 30, 2025, 7:33am UTC](https://community.hubspot.com/t/i-want-to-send-data-through-an-api-in-a-ui-extension-using-react/135387/2 "2025-06-30T07:33:43Z")

</div>

Hi @SagarTomar,

This issue can occur if you’re importing or passing a value incorrectly, perhaps you’re referencing a variable or object instead of a function. Double-check that the function you’re calling is actually a function. Also, review your imports and how you’re passing properties to ensure everything is set up correctly.

---

<div class="post-metadata">

**Author:** ![SagarTomar](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@SagarTomar](https://community.hubspot.com/u/SagarTomar)\
**Post date:** [June 30, 2025, 7:39am UTC](https://community.hubspot.com/t/i-want-to-send-data-through-an-api-in-a-ui-extension-using-react/135387/3 "2025-06-30T07:39:01Z")

</div>

Thank you for response .

if i send hardcoded value still not working

const response = await fetchApi(‘[https://mediumpurple-deer-340527.hostingersite.com/send-message](https://mediumpurple-deer-340527.hostingersite.com/send-message)’, {

```
method: 'POST',

headers: {

 'Content-Type': 'application/json',

},

body: JSON.stringify({

 number: "18315316612",

 portalId: "46022779",

 message: "hello",

}),

```

});

---

<div class="post-metadata">

**Author:** ![MKatewa](https://avatars.discourse-cdn.com/v4/letter/m/4af34b/32.png) [@MKatewa](https://community.hubspot.com/u/MKatewa)\
**Post date:** [July 9, 2025, 10:19pm UTC](https://community.hubspot.com/t/i-want-to-send-data-through-an-api-in-a-ui-extension-using-react/135387/4 "2025-07-09T22:19:38Z")

</div>

Hi @SagarTomar  
To make API requests within a HubSpot UI Extension, you should use hubspot.fetch() instead of the native fetch() method.  
I recommend reviewing the official documentation here:

> **[Fetching data for UI extensions - HubSpot docs](https://developers.hubspot.com/docs/apps/developer-platform/add-features/ui-extensions/fetching-data)**
>
> Learn the different ways to fetch data in UI extensions, including CRM hooks, actions, serverless functions, and the hubspot.fetch() API.

I hope this helps! If it resolves your issue, please consider marking this as the accepted solution and giving it an upvote to assist others in the community as well.

Thanks!

---

<div class="post-metadata">

**Author:** ![SagarTomar](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@SagarTomar](https://community.hubspot.com/u/SagarTomar)\
**Post date:** [July 3, 2025, 5:24am UTC](https://community.hubspot.com/t/i-want-to-send-data-through-an-api-in-a-ui-extension-using-react/135387/5 "2025-07-03T05:24:06Z")

</div>

if i am sending hardcode value same error is appering. the error is

72c72d32-09ab-45bf-9…f89deb5c56f468137:1 Error sending SMS: TypeError: i is not a function

at t.N (72c72d32-09ab-45bf-9…b5c56f468137:1:4633)

at t (index.js:403:30)

at Object.call (index.js:102:30)

at MessagePort.d (index.js:408:42)

N @ 72c72d32-09ab-45bf-9…f89deb5c56f468137:1

t @ index.js:403

call @ index.js:102

d @ index.js:408

---

<div class="post-metadata">

**Author:** ![GRajput](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/grajput/32/102020_2.png) [@GRajput](https://community.hubspot.com/u/GRajput)\
**Post date:** [July 1, 2025, 3:37pm UTC](https://community.hubspot.com/t/i-want-to-send-data-through-an-api-in-a-ui-extension-using-react/135387/6 "2025-07-01T15:37:52Z")

</div>

Hi @SagarTomar

“Please share the top part of your file (imports and how fetchApi is defined), I believe there must be an error in importing the file.  
Here are some tips -  
Use fetchApi only inside UI extensions and import it correctly.  
Use regular fetch() in non-extension code.”

I hope this will help you out. Please mark it as **Solution Accepted and upvote** to help another Community member.

Thanks!

---

<div class="post-metadata">

**Author:** ![SagarTomar](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@SagarTomar](https://community.hubspot.com/u/SagarTomar)\
**Post date:** [July 2, 2025, 10:44am UTC](https://community.hubspot.com/t/i-want-to-send-data-through-an-api-in-a-ui-extension-using-react/135387/7 "2025-07-02T10:44:50Z")

</div>

import React, { useState, useEffect, useRef } from ‘react’;

import {

Button,

Modal,

ModalBody,

Text,

TextArea,

Box,

Flex,

hubspot,

} from ‘@hubspot/ui-extensions’;

// hubspot.extend(({ context, actions }) =\> {

// console.log(‘✅ Available actions:’, actions);

// console.log(‘✅ fetchHubSpotApi:’, actions.fetchHubSpotApi);

// console.log(‘✅ Type of fetchHubSpotApi:’, typeof actions.fetchHubSpotApi);

// return (

// \<Extension

// context={context}

// fetchProperties={actions.fetchCrmObjectProperties}

// fetchHubSpotApi={actions.fetchHubSpotApi} // ✅ correctly pass

// /\>

// );

// });

hubspot.extend(({ context, actions }) =\> {

console.log(‘✅ Available actions:’, actions);

return (

```
\<Extension

  context={context}

  fetchProperties={actions.fetchCrmObjectProperties}

  // ❌ remove fetchHubSpotApi

/>

```

);

});

const Extension = ({ context, fetchProperties, fetchHubSpotApi }) =\> {

return (

```
\<Button

  overlay={

    \<Modal id="chatbot-modal" title="MCUTILITY" width="md">

      \<ModalBody>

        \<Box direction="column" gap="md" padding="md">

          \<ChatBotUI

            context={context}

            fetchProperties={fetchProperties}

            // fetchHubSpotApi={fetchHubSpotApi} // ✅ pass correctly

          />

        \</Box>

      \</ModalBody>

    \</Modal>

  }

>

  Send SMS

\</Button>

```

);

};

const ChatBotUI = ({ context, fetchProperties }) =\> {

const [messages, setMessages] = useState([

```
{ from: 'bot', text: 'Hi there! How can I assist you today?' },

```

]);

const refChatUi = useRef(null);

const [input, setInput] = useState(‘’);

const [contactDetails, setContactDetails] = useState(null);

const [loadingContact, setLoadingContact] = useState(true);

const [errorContact, setErrorContact] = useState(null);

const [sendingSMS, setSendingSMS] = useState(false);

const [smsError, setSmsError] = useState(null);

useEffect(() =\> {

```
const getContactData = async () => {

  if (

    context.crm &&

    context.crm.objectId &&

    context.crm.objectTypeId === '0-1'

  ) {

    try {

      setLoadingContact(true);

      const properties = await fetchProperties(\[

        'firstname',

        'lastname',

        'email',

        'phone',

        'company',

      ]);

      setContactDetails(properties);

    } catch (error) {

      setErrorContact('Failed to load contact details.');

    } finally {

      setLoadingContact(false);

    }

  } else {

    setErrorContact('Extension not running on a contact record.');

    setLoadingContact(false);

  }

};

getContactData();

```

}, [context, fetchProperties]);

useEffect(() =\> {

```
if (refChatUi.current) {

  refChatUi.current.scrollTop = refChatUi.current.scrollHeight;

}

```

}, [messages]);

const handleSend = async () =\> {

if (!input.trim()) return;

setMessages((prev) =\> […prev, { from: ‘user’, text: input }]);

if (!contactDetails || !contactDetails.phone) {

```
const err = 'Contact phone number not available.';

setSmsError(err);

setMessages((prev) => \[...prev, { from: 'bot', text: \`Error: ${err}\` }]);

setInput('');

return;

```

}

const phoneNumber = contactDetails.phone;

const portalId = context.portal?.id;

if (!portalId) {

```
const err = 'HubSpot portal ID not found.';

setSmsError(err);

setMessages((prev) => \[...prev, { from: 'bot', text: \`Error: ${err}\` }]);

setInput('');

return;

```

}

try {

```
setSendingSMS(true);

setSmsError(null);

// ✅ Call your own external API directly

```

const response = await fetch(‘[https://mediumpurple-deer-340527.hostingersite.com/send-message](https://mediumpurple-deer-340527.hostingersite.com/send-message)’, {

method: ‘POST’, // ✅ Correct method

headers: {

```
'Content-Type': 'application/json',

```

},

body: JSON.stringify({

```
number: phoneNumber,

portalId,

message: input,

```

}),

});

```
const result = await response.json();

if (!response.ok) {

  throw new Error(result.message \|\| 'Failed to send SMS');

}

setMessages((prev) => \[

  ...prev,

  {

    from: 'bot',

    text: \`SMS sent to ${phoneNumber}. You said: "${input}".\`,

  },

]);

setInput('');

```

} catch (error) {

```
const err = \`Failed to send SMS: ${error.message}\`;

setSmsError(err);

setMessages((prev) => \[...prev, { from: 'bot', text: err }]);

```

} finally {

```
setSendingSMS(false);

```

}

};

return (

```
\<Box direction="column">

  \<Box

    padding="sm"

    style={{

      border: '1px solid #e0e0e0',

      borderRadius: '8px',

      marginBottom: '12px',

    }}

  >

    \<Text variant="small" style={{ fontWeight: 'bold' }}>

      Contact Details:

    \</Text>

    {loadingContact && \<Text variant="small">Loading contact...\</Text>}

    {errorContact && (

      \<Text variant="small" style={{ color: 'red' }}>

        {errorContact}

      \</Text>

    )}

    {contactDetails && (

      \<Box direction="column" gap="extraSmall">

        {contactDetails.firstname && (

          \<Text variant="small">

            First Name: {contactDetails.firstname}

          \</Text>

        )}

        {contactDetails.lastname && (

          \<Text variant="small">

            Last Name: {contactDetails.lastname}

          \</Text>

        )}

        {contactDetails.email && (

          \<Text variant="small">Email: {contactDetails.email}\</Text>

        )}

        {contactDetails.phone && (

          \<Text variant="small">Phone: {contactDetails.phone}\</Text>

        )}

        {contactDetails.company && (

          \<Text variant="small">Company: {contactDetails.company}\</Text>

        )}

      \</Box>

    )}

  \</Box>

  {sendingSMS && (

    \<Text variant="small" style={{ color: 'blue' }}>

      Sending SMS...

    \</Text>

  )}

  {smsError && (

    \<Text variant="small" style={{ color: 'red' }}>

      {smsError}

    \</Text>

  )}

  \<Box

    direction="column"

    ref={refChatUi}

    style={{

      height: '300px',

      overflowY: 'auto',

      padding: '8px',

      border: '1px solid #ddd',

      borderRadius: '8px',

      marginBottom: '12px',

    }}

  >

    {messages.map((msg, idx) => (

      \<Flex

        key={idx}

        justifyContent={msg.from === 'user' ? 'flex-end' : 'flex-start'}

      >

        \<Box

          padding="sm"

          style={{

            backgroundColor:

              msg.from === 'user' ? '#DCF8C6' : '#F1F0F0',

            borderRadius: '16px',

            maxWidth: '70%',

          }}

        >

          \<Text>{msg.text}\</Text>

        \</Box>

      \</Flex>

    ))}

  \</Box>

  \<Box direction="column" gap="xs">

    \<TextArea

      placeholder="Type your message..."

      value={input}

      onChange={(value) => setInput(value)}

      rows={2}

      disabled={sendingSMS}

    />

    \<Button onClick={handleSend} disabled={sendingSMS}>

      Send

    \</Button>

  \</Box>

\</Box>

```

);

};

thank you for your response this is my full code

---

<div class="post-metadata">

**Author:** ![GRajput](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/grajput/32/102020_2.png) [@GRajput](https://community.hubspot.com/u/GRajput)\
**Post date:** [July 2, 2025, 2:28pm UTC](https://community.hubspot.com/t/i-want-to-send-data-through-an-api-in-a-ui-extension-using-react/135387/8 "2025-07-02T14:28:53Z")

</div>

Hi @SagarTomar

 ![GRajput_0-1751466490904.png](https://d2qpr2jzcazej7.cloudfront.net/original/2X/7/71929e104a46b5c4bf787dbffadc65311c8977d2.png)

In this section, try using await fetchHubSpotApi, as we are inside the HubSpot UI extension, so we should use this, and it should work fine. That’s the best I can get from your code; the rest, I believe, you are already doing all good here.

I hope this will help you out. Please mark it as **Solution Accepted and upvote** to help another Community member.

Thanks!

---

<div class="post-metadata">

**Author:** ![SagarTomar](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@SagarTomar](https://community.hubspot.com/u/SagarTomar)\
**Post date:** [July 3, 2025, 5:13am UTC](https://community.hubspot.com/t/i-want-to-send-data-through-an-api-in-a-ui-extension-using-react/135387/9 "2025-07-03T05:13:50Z")

</div>

thank you for response

dca557ab-453a-42d2-8…7e14ff94ca2f085ce:1 Refused to connect to ‘[https://mediumpurple-deer-340527.hostingersite.com/send-message](https://mediumpurple-deer-340527.hostingersite.com/send-message)’ because it violates the following Content Security Policy directive: “connect-src none”.

N @ dca557ab-453a-42d2-8…7e14ff94ca2f085ce:1

t @ index.js:403

call @ index.js:102

d @ index.js:408

dca557ab-453a-42d2-8…7e14ff94ca2f085ce:1 Fetch API cannot load [https://mediumpurple-deer-340527.hostingersite.com/send-message](https://mediumpurple-deer-340527.hostingersite.com/send-message). Refused to connect because it violates the document’s Content Security Policy.

N @ dca557ab-453a-42d2-8…7e14ff94ca2f085ce:1

t @ index.js:403

call @ index.js:102

d @ index.js:408

 ![SagarTomar_0-1751519472131.png](https://d2qpr2jzcazej7.cloudfront.net/original/2X/9/9c2fe610b089efa0db12b73e425fcd33eadc072c.png)

now i am facing this error . this api written in laravel.please help me with this issue
