Getting Data from Help Scout
Getting Data from Help Scout with JavaScript SDK
In this guide, you’ll learn how to access Help Scout data using the HelpScout.getApplicationContext()
method provided by the JavaScript SDK. This method allows you to fetch contextual information from the current Help Scout environment, such as company, conversation, customer, mailbox, and user details.
Prerequisites
Ensure that you have completed the Getting Started Guide and set up a Help Scout App.
Fetching Context Data
To fetch context data, simply call the getApplicationContext()
method in your app. This method returns a Promise
that resolves with an object containing the following properties: company
, conversation
, customer
, mailbox
, and user
.
Here’s an example of how to use getApplicationContext()
:
import HelpScout from '@helpscout/javascript-sdk';
HelpScout.getApplicationContext().then((context) => {
console.log('Help Scout Context:', context);
});
You can access specific properties of the context object by using dot notation or destructuring assignment. Here are some examples:
Accessing the user’s first name
HelpScout.getApplicationContext().then(({ user }) => {
console.log('User First Name:', user?.firstName);
});
Accessing the customer’s email
HelpScout.getApplicationContext().then(({ customer }) => {
console.log('Customer Email:', customer?.emails?.[0]?.value);
});
Accessing the conversation subject
HelpScout.getApplicationContext().then(({ conversation }) => {
console.log('Conversation Subject:', conversation?.subject);
});
🚀 Dive Deeper into Context Objects
After you’ve familiarized yourself with the examples above, we encourage you to dive deeper into the full details of the different context objects. Understanding these objects and their properties will enable you to build more powerful and customized integrations with Help Scout. Don’t hesitate to explore the comprehensive documentation for each object:
By leveraging the richness of these objects, you can unlock the full potential of the Help Scout JavaScript SDK and create exceptional experiences for your users. Get started now and discover the possibilities!
Handling Errors
Since getApplicationContext()
returns a Promise
, you can use the .catch()
method to handle errors:
HelpScout.getApplicationContext()
.then((context) => {
console.log('Help Scout Context:', context);
})
.catch((error) => {
console.error('Error fetching Help Scout Context:', error);
});
That’s it! You now know how to fetch and access Help Scout context data using the getApplicationContext()
method from the JavaScript SDK. Happy coding!
Using Context Provider in React
In this guide, you’ll learn how to use the useHelpScoutContext()
hook and the <HelpScoutContextProvider>
component from the @helpscout/ui-kit
package to access Help Scout context data in a React application.
Prerequisites
Ensure that you have completed the Getting Started Guide and set up a Help Scout App using React.
Set up Context Provider
First, you’ll need to wrap your root component with the <HelpScoutContextProvider>
component. This component should be placed in your main.tsx
file, where the root of your React application is rendered:
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
import { HelpScoutContextProvider } from "@helpscout/ui-kit";
ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
<React.StrictMode>
<HelpScoutContextProvider>
<App />
</HelpScoutContextProvider>
</React.StrictMode>
);
Using Context Provider hook
Now that the <HelpScoutContextProvider>
is set up, you can use the useHelpScoutContext()
hook within your components to access Help Scout context data. Like the getApplicationContext()
method described above, this hook returns an object containing the following properties: company
, conversation
, customer
, mailbox
, and user
. You can find the full details about these objects below ⤵️.
Here’s an example of how to use the useHelpScoutContext()
hook in your App.tsx
file:
import { DefaultStyle, Heading, useHelpScoutContext } from "@helpscout/ui-kit";
function App() {
const { user } = useHelpScoutContext();
return (
<div className="App">
<DefaultStyle />
<Heading level="h4">Hi {user?.firstName || "there"} 👋</Heading>
</div>
);
}
export default App;
In the example above, the useHelpScoutContext()
hook is used to access the user
property from the Help Scout context. The user’s first name is then displayed in a <Heading>
component.
That’s it! You now know how to use the useHelpScoutContext()
hook and the <HelpScoutContextProvider>
component to access Help Scout context data in a React application. Happy coding!
The Context Object
Property | Type | Description |
---|---|---|
Context | ||
company | Company | Information about the current company. |
conversation | Conversation | Information about the current conversation. |
customer | Customer | Information about the current customer. |
mailbox | Mailbox | Information about the current mailbox. |
user | User | Information about the current user. |
Company
Property | Type | Description |
---|---|---|
Company | ||
id | number | The company’s unique identifier. |
Conversation
Property | Type | Description |
---|---|---|
Conversation | ||
id | number | The conversation’s unique identifier. |
assignee | Assignee | The user assigned to the conversation. |
customerId | number | The main customer’s unique identifier. |
customers | ConversationCustomer[] | An array of customers related to the conversation. |
customFields | CustomField[] | An array of custom fields associated with the conversation. |
following | boolean | Indicates if the current user is following the conversation. |
mailboxId | number | The unique identifier of the mailbox the conversation belongs to. |
number | number | The conversation’s number within the mailbox. |
status | Status | The current status of the conversation. |
subject | string | The subject of the conversation. |
tags | Tag[] | An array of tags associated with the conversation. |
Assignee | ||
id | number | The assignee’s unique identifier. |
first | string | The assignee’s first name. |
last | string | The assignee’s last name. |
type | string | The assignee’s type (e.g., ‘user’, ‘team’). |
string | The assignee’s email address. | |
ConversationCustomer | ||
id | number | The customer’s unique identifier. |
firstName | string | The customer’s first name. |
lastName | string | The customer’s last name. |
emails | ConversationCustomerEmail[] | An array of customer’s email addresses. |
ConversationCustomerEmail | ||
id | number | The email’s unique identifier. |
value | string | The email address. |
default | boolean | Indicates if the email is the customer’s default email. |
CustomField | ||
id | number | The custom field’s unique identifier. |
name | string | The custom field’s name. |
value | string | The custom field’s value. |
text | string | The custom field’s text representation. |
Tag | ||
id | number | The tag’s unique identifier. |
tag | string | The tag’s text. |
Status | Description |
---|---|
active | The conversation is active and ongoing. |
pending | The conversation is pending further action. |
closed | The conversation is closed. |
spam | The conversation is marked as spam. |
Customer
Property | Type | Description |
---|---|---|
Customer | ||
id | number | The customer’s unique identifier. |
firstName | string | The customer’s first name. |
lastName | string | The customer’s last name. |
emails | Email[] | An array of the customer’s email addresses. |
phones | Phone[] | An array of the customer’s phone numbers. |
company | string (optional) | The customer’s company. |
jobTitle | string (optional) | The customer’s job title. |
photoUrl | string | The URL of the customer’s photo. |
conversationCount | number | The number of conversations the customer is involved in. |
note | string | The note associated with the customer’s profile. |
properties | CustomerProperty[] | An array of the customer’s properties. |
socialProfiles | SocialProfile[] | An array of the customer’s social profiles. |
websites | Website[] | An array of the customer’s websites. |
id | number | The email’s unique identifier. |
value | string | The email address. |
Phone | ||
id | number | The phone’s unique identifier. |
value | string | The phone number. |
type | PhoneType | The phone number’s type (e.g., ‘work’, ‘home’, ‘mobile’, ‘fax’, ‘pager’, ‘other’). |
CustomerProperty | ||
slug | string | The property’s slug. |
name | string | The property’s name. |
value | string (optional) | The property’s value. |
text | string (optional) | The property’s text representation. |
type | CustomerPropertyType | The property’s type (e.g., ‘text’, ‘number’, ‘url’, ‘date’, ‘dropdown’). |
SocialProfile | ||
id | number | The social profile’s unique identifier. |
type | SocialProfileType | The social profile’s type (e.g., ‘facebook’, ‘twitter’, ‘linkedin’, ‘other’). |
url | string | The social profile’s URL. |
Website | ||
id | number | The website’s unique identifier. |
value | string | The website’s URL. |
Mailbox
Property | Type | Description |
---|---|---|
Mailbox | ||
id | number | The mailbox’s unique identifier. |
name | string | The mailbox’s name. |
string | The mailbox’s email address. |
User
Property | Type | Description |
---|---|---|
User | ||
id | number | The user’s unique identifier. |
firstName | string | The user’s first name. |
lastName | string | The user’s last name. |
string | The user’s email address. | |
initials | string | The user’s initials. |
photoUrl | string | The URL of the user’s photo. |
isAdmin | boolean | Indicates if the user has admin privileges. |