Widget

Generate JWT Token

The widget only functions when a user is authenticated: only authenticated users can read and write to their own documents and make requests. To authenticate your users, you need to first get a JWT token containing the user uid (a string or number that uniquely identifies the user on your backend), a daily limit for the user, and whether this user has access to the screener or not. You can hit our endpoint from your servers to retrieve a token for a given user:

curl --request POST
--url https://api.widget.enterprise.io/auth/generate-token
--header 'Content-Type: application/json'
--header 'Accept: text/plain'
--header 'Authorization: Bearer YOUR_FINCHAT_API_KEY'
--data '{ "payload": { "uid": "USER_UID", "dailyLimit": 5, "screener": false } }'
export default async function () {
const apiKey = 'YOUR_API_KEY';
const payload = { user: 'YOUR_USER_UID', dailyLimit: 5, screener: false };
const res = await fetch('https://api.enterprise.finchat.io/auth/generate-token', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer undefined'
},
body: JSON.stringify({ payload })
});
if (res.status !== 200) return new Response(res.statusText, { status: res.status });
const token = await res.text();
return new Response(token, { status: 200 })
}

Embed Widget

You can fetch this token from your frontend and send us the token via postMessage, which is a secure mechanism for cross-origin communications. The postMessage method is a part of the Web API provided by browsers to allow secure communication between windows or iframes, especially when they come from different origins. This is because the origin property that indicates the sender's origin means we can verify the sender's origin before processing the message.

import React from 'react';
const Widget = () => {
const token = 'YOUR_SIGNED_TOKEN';
const widgetKey = 'YOUR_WIDGET_KEY';
React.useEffect(() => {
function handleIframeReady(event: MessageEvent) {
if (event.data === 'READY') {
const targetWindow = window.frames['finchat'];
if (!targetWindow) return;
targetWindow.postMessage({ token }, 'https://enterprise.finchat.io');
}
}
window.addEventListener('message', handleIframeReady);
return () => window.removeEventListener('message', handleIframeReady);
}, [token]);
return (
<iframe
name="finchat"
src={`https://enterprise.finchat.io/${widgetKey}`}
style={{ border: 'none', width: '100%', height: '100%' }}
/>
);
};
export default Widget;

We listen for messages and check the origin of the requests against the domains you have provided in the FinChat Admin Portal before processing them. We send the token to our backend where: (1) your credentials are retrieved, (2) the API Key is used to extract the contents of the token, and (3) a Firebase Custom Token is minted that is then sent to our frontend to authenticate the user. We then send this token to the frontend to authenticate the user.

User Data

Your data is stored separately from other customers, in a collection dedicated to your organization, and user data is secured as follows: only authenticated users from your own organization can read and write to their own documents. This is enforced using Firebase Rules.`

For Cloud Firestore and Cloud Storage, Rules use the following syntax:

rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /customers/{org}/users/{uid}/{anyUserFile=**} {
allow read, write: if request.auth != null && request.auth.token.org == org && request.auth.uid == uid;
}
}
}

This section is password protected.

Retrieve User Data

To get the most out of this guide, you should first embed the widget in your application.

Once you have embedded the widget, you can retrieve usage data for your users. You can send a GET Request with your API Key and paginate through the users (each page contains up to 100 users) using the page query parameter, like so:

curl 'https://finchat.io/api/organization/data?apiKey=YOUR_FINCHAT_API_KEY&page=1'

The response is an array of users, with each user having a user object which contains their uid, an array of timestamps used for throlling usage, and their sidebar navigation chats. Each user also has an array of chats, which contains the chat uid, title, and an array of messages. Here is what an export of a user looks like:

[
{
"user": {
"uid": "eb136c833b22490cab0b63d58e4a81fa-e68faef5eb1f40b3be4750bead8a9faf",
"navChats": {
"favorites": [],
"chats": [
{
"uid": "2c3a51d7-5bf4-4527-801e-73056d2db7a6",
"title": "Berkshire's Diverse Revenue Sources",
"favorite": false
},
{
"uid": "a60dbaae-6b21-44fc-afd0-2c83219bbb95",
"title": "Tesla's 2022 Car Sales",
"favorite": false
},
{
"uid": "2f7d5267-de78-4b06-ba21-15891736aeb1",
"title": "Microsoft's Revenue Streams",
"favorite": false
}
]
},
"tracking": {
"timestamps": [
1696152193258,
1696153448750,
1696153484339,
1696153505613
]
}
},
"chats": [
{
"uid": "2c3a51d7-5bf4-4527-801e-73056d2db7a6",
"title": "Berkshire's Diverse Revenue Sources",
"messages": [
{
"createdAt": "2023-10-01T09:44:32.066Z",
"role": "user",
"id": "777bf416-6e9e-49c6-90fa-7c9885dfe692",
"content": "What are Berkshire's various revenue streams?",
"updatedAt": "2023-10-01T09:44:32.066Z"
},
{
"summary": "Berkshire Hathaway has various revenue streams, including manufacturing, service and retailing, insurance, and other segments. The most recent data shows that manufacturing revenue was $75.78 billion, and service and retailing revenue was $38.3 billion for the fiscal year ending on December 31, 2022. The growth rates for these revenue streams are not provided in the data. Berkshire Hathaway also generates revenue from segments such as GEICO Insurance, BHE, Berkshire Hathaway Primary Group, Berkshire Hathaway Reinsurance Group, and Pilot Travel Centers. The revenue for these segments varies, and the most recent data for each segment is available in the data sources.",
"createdAt": "2023-10-01T09:44:37.399Z",
"role": "bot",
"aborted": false,
"id": "57f4774e-05db-4447-b395-b3bc3775838a",
"content": "Based on the available data, Berkshire Hathaway has various revenue streams. The main revenue streams include manufacturing, service and retailing, GEICO Insurance, BHE, Berkshire Hathaway Primary Group, Berkshire Hathaway Reinsurance Group, and Pilot Travel Centers.
In terms of manufacturing revenue, Berkshire Hathaway's revenue has shown growth over the years. The most recent data for manufacturing revenue is $75.78 billion for the fiscal year ending on December 31, 2022. However, the growth rate for manufacturing revenue is not provided in the data.
For service and retailing revenue, Berkshire Hathaway's revenue has also shown growth. The most recent data for service and retailing revenue is $38.3 billion for the fiscal year ending on December 31, 2022. The growth rate for service and retailing revenue is not provided in the data.
Additionally, Berkshire Hathaway has revenue streams from other segments such as GEICO Insurance, BHE, Berkshire Hathaway Primary Group, Berkshire Hathaway Reinsurance Group, and Pilot Travel Centers. The revenue for these segments varies, and the most recent data for each segment is provided in the data sources.
It's important to note that the growth rates for each revenue stream are also provided in the data, indicating the growth trends over the years.
Overall, Berkshire Hathaway has a diverse range of revenue streams, including manufacturing, service and retailing, insurance, and other segments.",
"updatedAt": "2023-10-01T09:44:37.399Z",
"relatedQuestions": [
"What are the main sources of revenue for Berkshire Hathaway?",
"How has the composition of Berkshire's revenue streams changed over time?",
"What are the key factors driving growth in Berkshire's different revenue streams?"
]
},
{
"createdAt": "2023-10-01T09:44:54.042Z",
"role": "user",
"id": "2b50a12f-3717-4667-88b3-5f037fdefe97",
"content": "what does the operating profit breakdown look like?",
"updatedAt": "2023-10-01T09:44:54.042Z"
},
{
"summary": "Berkshire Hathaway has various revenue streams, including manufacturing, service and retailing, insurance, and other segments. The most recent data shows that manufacturing revenue was $75.78 billion, and service and retailing revenue was $38.3 billion for the fiscal year ending on December 31, 2022. The growth rates for these revenue streams are not provided in the data.
In terms of profit before taxes, for the fiscal year ending on December 31, 2022, Berkshire Hathaway Primary Group had a profit of $393 million, representing a growth rate of 54.45% compared to the previous year. Berkshire Hathaway Reinsurance Group had a profit of $1,389 million, with no growth rate provided.
Unfortunately, the operating profit breakdown for Berkshire Hathaway as a whole is not explicitly provided in the available data. However, based on the information available, it can be seen that the Berkshire Hathaway Primary Group and Berkshire Hathaway Reinsurance Group are significant segments contributing to the overall profit of the company.",
"createdAt": "2023-10-01T09:45:00.133Z",
"role": "bot",
"aborted": false,
"id": "adea7cd1-774f-48f1-9948-5784cec9781d",
"content": "Based on the available data, the operating profit breakdown for Berkshire Hathaway is not explicitly provided. However, we do have information on the profit before taxes for various segments of Berkshire Hathaway, such as Manufacturing, Service and Retailing, Berkshire Hathaway Primary Group, Berkshire Hathaway Reinsurance Group, and others.
For the most recent fiscal year ending on December 31, 2022, the profit before taxes for Berkshire Hathaway Primary Group was $393 million. This represents a growth rate of 54.45% compared to the previous year.
For Berkshire Hathaway Reinsurance Group, the profit before taxes for the same period was $1,389 million, with no growth rate provided.
Unfortunately, we don't have specific information on the operating profit breakdown for Berkshire Hathaway as a whole. However, based on the available data, we can see that the Berkshire Hathaway Primary Group and Berkshire Hathaway Reinsurance Group are two significant segments contributing to the overall profit of the company.",
"updatedAt": "2023-10-01T09:45:00.133Z",
"relatedQuestions": [
"What is the operating profit for each revenue stream of Berkshire Hathaway?",
"How does the operating profit of the manufacturing segment compare to the service and retailing segment?",
"Are there any segments within Berkshire Hathaway that have shown significant growth in operating profit?"
]
}
]
},
{
"uid": "2f7d5267-de78-4b06-ba21-15891736aeb1",
"title": "Microsoft's Revenue Streams",
"messages": [
{
"createdAt": "2023-10-01T09:23:00.819Z",
"role": "user",
"id": "327a9857-0420-45bf-aaf4-2759ebe687bf",
"content": "How does Microsoft make money?",
"updatedAt": "2023-10-01T09:23:00.819Z"
},
{
"summary": "Microsoft makes money through various revenue streams. One of the main sources of revenue is their Intelligent Cloud segment, which includes services like Azure, server products, and enterprise services. The Intelligent Cloud segment generated $87.91 billion in revenue for the fiscal year ending on June 30, 2023. Another significant revenue stream is their Productivity & Business Processes segment, which includes products like Office, LinkedIn, and Dynamics. This segment generated $69.27 billion in revenue. Microsoft also generates revenue from their More Personal Computing segment, which includes products like Windows, Surface, Xbox, and search advertising. This segment generated $54.73 billion in revenue. Additionally, Microsoft earns revenue from their Search and News Advertising segment, which focuses on advertising revenue from search engines and news platforms, generating $12.21 billion in revenue. Lastly, their Microsoft Cloud segment, which includes services like Office 365, Azure, and Dynamics 365, generated $111.6 billion in revenue. In summary, Microsoft makes money primarily through its Intelligent Cloud, Productivity & Business Processes, More Personal Computing, Search and News Advertising, and Microsoft Cloud segments.",
"createdAt": "2023-10-01T09:23:04.614Z",
"role": "bot",
"aborted": false,
"id": "67f5934f-7872-4101-89ae-3c311982ee3a",
"content": "Microsoft makes money through various revenue streams. One of the main sources of revenue is their Intelligent Cloud segment, which includes services like Azure, server products, and enterprise services. According to the most recent data, the Intelligent Cloud segment generated $87.91 billion in revenue for the fiscal year ending on June 30, 2023. This segment has shown strong growth over the years, with a three-year growth rate of 46.32% and a five-year growth rate of 125.49%.
Another significant revenue stream for Microsoft is their Productivity & Business Processes segment, which includes products like Office, LinkedIn, and Dynamics. This segment generated $69.27 billion in revenue for the fiscal year ending on June 30, 2023. The Productivity & Business Processes segment has also experienced growth, with a three-year growth rate of 28.49% and a five-year growth rate of 68.3%.
Additionally, Microsoft generates revenue from their More Personal Computing segment, which includes products like Windows, Surface, Xbox, and search advertising. The More Personal Computing segment generated $54.73 billion in revenue for the fiscal year ending on June 30, 2023. This segment has shown modest growth, with a three-year growth rate of 1.18% and a five-year growth rate of 19.77%.
Furthermore, Microsoft earns revenue from their Search and News Advertising segment, which focuses on advertising revenue from search engines and news platforms. According to the most recent data, this segment generated $12.21 billion in revenue. The Search and News Advertising segment has experienced growth, with a three-year growth rate of 31.74% and a five-year growth rate of 60.04%.
Lastly, Microsoft's Microsoft Cloud segment, which includes services like Office 365, Azure, and Dynamics 365, is another significant revenue generator. The Microsoft Cloud segment generated $111.6 billion in revenue for the fiscal year ending on June 30, 2023. This segment has shown strong growth, with a three-year growth rate of 61.51% and a five-year growth rate of 192.91%.
In summary, Microsoft makes money primarily through its Intelligent Cloud, Productivity & Business Processes, More Personal Computing, Search and News Advertising, and Microsoft Cloud segments. These segments offer a range of products and services that cater to businesses and consumers, contributing to Microsoft's overall revenue growth.",
"updatedAt": "2023-10-01T09:23:04.614Z",
"relatedQuestions": [
"What are the main sources of revenue for Microsoft?",
"How does Microsoft's business model generate profits?",
"What are the key factors driving Microsoft's financial success?"
]
}
]
},
{
"uid": "a60dbaae-6b21-44fc-afd0-2c83219bbb95",
"title": "Tesla's 2022 Car Sales",
"messages": [
{
"createdAt": "2023-10-01T09:44:00.354Z",
"role": "user",
"id": "f98ce9c1-dc71-4a81-8fab-6d2a699f8780",
"content": "How many cars did Tesla sell in 2022?",
"updatedAt": "2023-10-01T09:44:00.354Z"
},
{
"summary": "The user asked how many cars Tesla sold in 2022. The AI responded by stating that the exact number of cars sold in 2022 is not available. However, it provided information on Tesla's automotive sales revenue for the fiscal year ending on December 31, 2022, which was $67.21 billion, a significant increase compared to the previous year. The AI mentioned that without additional information such as the average selling price of Tesla vehicles, it is not possible to accurately estimate the number of cars sold in 2022.",
"createdAt": "2023-10-01T09:44:05.531Z",
"role": "bot",
"aborted": false,
"id": "4aa585f3-c3d5-407e-9f36-22d6f0e73e82",
"content": "Based on the available data, we do not have the exact number of cars Tesla sold in 2022. However, we can see that Tesla's automotive sales revenue for the fiscal year ending on December 31, 2022, was $67.21 billion. This represents a significant increase compared to the previous year's revenue of $44.13 billion.
It's important to note that the data provided does not explicitly state the number of cars sold. Instead, it focuses on revenue figures. To determine the number of cars sold, we would need additional information such as the average selling price of Tesla vehicles during that period. Without this information, we cannot provide an accurate estimate of the number of cars sold in 2022.",
"updatedAt": "2023-10-01T09:44:05.531Z",
"relatedQuestions": [
"What was the total revenue generated by Tesla from car sales in 2022?",
"How does the number of cars sold by Tesla in 2022 compare to previous years?",
"What is the average selling price of Tesla cars in 2022?"
]
}
]
}
]
}
]

Remove Users

To remove users from your widget, you can send a post request to https://finchat.io/api/organization/data/removeUser with you API Key and the user's uid, like so:

import fetch from 'node-fetch'; // or another fetch library if you're in a Node environment
const removeUser = async (apiKey: string, userUid: string): Promise<Response> => {
const response = await fetch('https://finchat.io/api/organization/data/removeUser', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': Bearer undefined
},
body: JSON.stringify({ uid: userUid })
});
return response;
};

Manage User Access

Administrators can control user permissions and access to specialized features such as the screener. This is achieved during the generation of the JWT (JSON Web Token). The JWT token is crafted to include specific claims that dictate the user's unique identifier (UID) and their access privileges.

Widget Enhancements

Add Source Button

Add a "Source" button to the widget which will send metadata up to the parent window.

To add a source button to the widget, you must enable this option in the FinChat Partner Portal. Once enabled, clicking this button in the iframe will send a postMessage to the parent window. You must listen to this message with an event listener:

function captureSourceButton(event: MessageEvent) {
// Handle your custom code here
}
window.addEventListener('message', captureSourceButton);

The event object will contain data from the content card. This data will be one of the following types:

export interface Financials {
ticker: string
name: string
type: 'Financials'
metric: string
}
export interface Ratios {
ticker: string
name: string
type: 'Ratios'
metric: string
}
export interface Dividends {
ticker: string
name: string
type: 'Dividends'
}
export interface Transcripts {
ticker: string
name: string
label: string
type: 'Transcripts'
}
export interface Filings {
ticker: string
name: string
label: string
type: 'Filings'
}
export interface Segments {
ticker: string
name: string
label: string
type: 'Segments & KPIs'
metric: string
}
export interface Prices {
ticker: string
name: string
type: 'Prices'
}
export interface InvestorHoldings {
type: 'Investor Holdings'
cik: string
label: string
}