This document provides detailed information about the functions and components exported by the @vectorize-io/vectorize-connect
package.
Creates an OAuth popup window for Google authentication.
function startGDriveOAuth(config: OAuthConfig): Window | null
Parameters:
config
: AnOAuthConfig
object containing:clientId
: Your Google OAuth client IDclientSecret
: Your Google OAuth client secretapiKey
: Your Google API keyredirectUri
: The URI to redirect to after authenticationscopes
(optional): Array of OAuth scopes (defaults to drive.file)onSuccess
: Callback function when authentication succeedsonError
: Callback function when authentication fails
Returns:
- A
Window
object representing the popup window, ornull
if the popup couldn't be created
Example:
const popup = startGDriveOAuth({
clientId: process.env.GOOGLE_OAUTH_CLIENT_ID!,
clientSecret: process.env.GOOGLE_OAUTH_CLIENT_SECRET!,
apiKey: process.env.GOOGLE_API_KEY!,
redirectUri: `${window.location.origin}/api/google-callback`,
onSuccess: (selection) => {
console.log('Selected files:', selection.fileIds);
console.log('Refresh token:', selection.refreshToken);
},
onError: (error) => {
console.error('OAuth error:', error.message);
}
});
Creates a response for the OAuth callback page. This function is typically used in a Next.js API route to handle the OAuth redirect.
async function createGDrivePickerCallbackResponse(
code: string,
config: OAuthConfig,
error?: string | OAuthError
): Promise<Response>
Parameters:
code
: Authorization code from the OAuth redirectconfig
: AnOAuthConfig
objecterror
(optional): Error from the OAuth process
Returns:
- A
Response
object with the callback page HTML
Example:
// In a Next.js API route
export async function GET(request: NextRequest) {
const searchParams = request.nextUrl.searchParams;
const code = searchParams.get('code');
const error = searchParams.get('error');
const config = {
clientId: process.env.GOOGLE_OAUTH_CLIENT_ID!,
clientSecret: process.env.GOOGLE_OAUTH_CLIENT_SECRET!,
apiKey: process.env.GOOGLE_API_KEY!,
redirectUri: `${process.env.NEXT_PUBLIC_BASE_URL}/api/google-callback`
};
return createGDrivePickerCallbackResponse(
code || '',
config,
error || undefined
);
}
Redirects to the Vectorize platform's Google Drive connection page using a one-time token for security. This is used for non-white-label integration.
function redirectToVectorizeGoogleDriveConnect(
oneTimeToken: string,
organizationId: string,
platformUrl: string = 'https://platform.vectorize.io'
): Promise<void>
Parameters:
oneTimeToken
: A one-time security token generated usinggetOneTimeConnectorToken
organizationId
: Your Vectorize organization IDplatformUrl
(optional): URL of the Vectorize platform (defaults to 'https://platform.vectorize.io')
Returns:
- A
Promise
that resolves when the iframe is closed
Example:
const handleConnectGoogleDrive = async () => {
try {
// Get one-time token from API endpoint
const tokenResponse = await fetch(`/api/get-one-time-connector-token?userId=user123&connectorId=connector-id`)
.then(response => {
if (!response.ok) {
throw new Error(`Failed to generate token. Status: ${response.status}`);
}
return response.json();
});
// Then use the token to redirect to the Google Drive connect page
await redirectToVectorizeGoogleDriveConnect(
tokenResponse.token,
'your-org-id',
'https://platform.vectorize.io' // Optional
);
console.log('Google Drive connection completed');
} catch (err) {
console.error('Google Drive connection error:', err);
}
};
Creates an OAuth popup window for Dropbox authentication.
function startDropboxOAuth(config: DropboxOAuthConfig): Window | null
Parameters:
config
: ADropboxOAuthConfig
object containing:appKey
: Your Dropbox App keyappSecret
: Your Dropbox App secretredirectUri
: The URI to redirect to after authenticationscopes
(optional): Array of OAuth scopes (defaults to ["files.metadata.read", "files.content.read"])onSuccess
: Callback function when authentication succeedsonError
: Callback function when authentication fails
Returns:
- A
Window
object representing the popup window, ornull
if the popup couldn't be created
Example:
const popup = startDropboxOAuth({
appKey: process.env.DROPBOX_APP_KEY!,
appSecret: process.env.DROPBOX_APP_SECRET!,
redirectUri: `${window.location.origin}/api/dropbox-callback`,
onSuccess: (selection) => {
console.log('Selected files:', selection.selectedFiles);
console.log('Refresh token:', selection.refreshToken);
},
onError: (error) => {
console.error('OAuth error:', error.message);
}
});
Creates a response for the OAuth callback page. This function is typically used in a Next.js API route to handle the OAuth redirect.
async function createDropboxPickerCallbackResponse(
code: string,
config: DropboxOAuthConfig,
error?: string | OAuthError
): Promise<Response>
Parameters:
code
: Authorization code from the OAuth redirectconfig
: ADropboxOAuthConfig
objecterror
(optional): Error from the OAuth process
Returns:
- A
Response
object with the callback page HTML
Example:
// In a Next.js API route
export async function GET(request: NextRequest) {
const searchParams = request.nextUrl.searchParams;
const code = searchParams.get('code');
const error = searchParams.get('error');
const config = {
appKey: process.env.DROPBOX_APP_KEY!,
appSecret: process.env.DROPBOX_APP_SECRET!,
redirectUri: `${process.env.NEXT_PUBLIC_BASE_URL}/api/dropbox-callback`
};
return createDropboxPickerCallbackResponse(
code || '',
config,
error || undefined
);
}
Redirects to the Vectorize platform's Dropbox connection page using a one-time token for security. This is used for non-white-label integration.
function redirectToVectorizeDropboxConnect(
oneTimeToken: string,
organizationId: string,
platformUrl: string = 'https://platform.vectorize.io'
): Promise<void>
Parameters:
oneTimeToken
: A one-time security token generated usinggetOneTimeConnectorToken
organizationId
: Your Vectorize organization IDplatformUrl
(optional): URL of the Vectorize platform (defaults to 'https://platform.vectorize.io')
Returns:
- A
Promise
that resolves when the iframe is closed
Example:
const handleConnectDropbox = async () => {
try {
// Get one-time token from API endpoint
const tokenResponse = await fetch(`/api/get-one-time-connector-token?userId=user123&connectorId=connector-id`)
.then(response => {
if (!response.ok) {
throw new Error(`Failed to generate token. Status: ${response.status}`);
}
return response.json();
});
// Then use the token to redirect to the Dropbox connect page
await redirectToVectorizeDropboxConnect(
tokenResponse.token,
'your-org-id',
'https://platform.vectorize.io' // Optional
);
console.log('Dropbox connection completed');
} catch (err) {
console.error('Dropbox connection error:', err);
}
};
Creates a popup for file selection using an existing refresh token. This is typically used after a user has already authenticated.
async function startGDriveFileSelection(
config: OAuthConfig,
refreshToken: string,
targetWindow?: Window
): Promise<Window | null>
Parameters:
config
: AnOAuthConfig
objectrefreshToken
: An existing refresh token to usetargetWindow
(optional): Window to use instead of creating a new popup
Returns:
- A
Promise
that resolves to aWindow
object representing the popup window, ornull
if the popup couldn't be created
Example:
const handleSelectMoreFiles = async () => {
const config = {
clientId: process.env.GOOGLE_OAUTH_CLIENT_ID!,
clientSecret: process.env.GOOGLE_OAUTH_CLIENT_SECRET!,
apiKey: process.env.GOOGLE_API_KEY!,
redirectUri: `${window.location.origin}/api/google-callback`,
onSuccess: (selection) => {
console.log('Additional files selected:', selection.fileIds);
},
onError: (error) => {
console.error('Selection error:', error.message);
}
};
const popup = await startGDriveFileSelection(config, existingRefreshToken);
};
Creates a popup for file selection using an existing refresh token. This is typically used after a user has already authenticated.
async function startDropboxFileSelection(
config: DropboxOAuthConfig,
refreshToken: string,
selectedFiles?: Record<string, { name: string; mimeType: string; path?: string }>,
targetWindow?: Window
): Promise<Window | null>
Parameters:
config
: ADropboxOAuthConfig
objectrefreshToken
: An existing refresh token to useselectedFiles
(optional): Previously selected files to pre-populate the selectiontargetWindow
(optional): Window to use instead of creating a new popup
Returns:
- A
Promise
that resolves to aWindow
object representing the popup window, ornull
if the popup couldn't be created
Example:
const handleSelectMoreFiles = async () => {
const config = {
appKey: process.env.DROPBOX_APP_KEY!,
appSecret: process.env.DROPBOX_APP_SECRET!,
redirectUri: `${window.location.origin}/api/dropbox-callback`,
onSuccess: (selection) => {
console.log('Additional files selected:', selection.selectedFiles);
},
onError: (error) => {
console.error('Selection error:', error.message);
}
};
const popup = await startDropboxFileSelection(
config,
existingRefreshToken,
existingSelectedFiles // Optional
);
};
Creates a Google Drive OAuth Connector Source via the Vectorize API.
async function createGDriveSourceConnector(
config: VectorizeAPIConfig,
whiteLabel: boolean,
connectorName: string,
platformUrl: string = "https://api.vectorize.io/v1",
clientId?: string,
clientSecret?: string
): Promise<Response>
Parameters:
config
: AVectorizeAPIConfig
object containing:organizationId
: Your Vectorize organization IDauthorization
: Your Vectorize API key
whiteLabel
: Whether to create a white-label connectorconnectorName
: Name for the connectorplatformUrl
(optional): URL of the Vectorize API (defaults to 'https://api.vectorize.io/v1')clientId
(optional): Required for white-label connectorsclientSecret
(optional): Required for white-label connectors
Returns:
- A
Promise
that resolves with the connector ID
Example:
// Create a non-white-label connector
const connectorId = await createGDriveSourceConnector(
{
organizationId: process.env.VECTORIZE_ORG!,
authorization: process.env.VECTORIZE_TOKEN!
},
false, // non-white-label
"My Google Drive Connector",
"https://api.vectorize.io/v1"
);
// Create a white-label connector
const whitelabelConnectorId = await createGDriveSourceConnector(
{
organizationId: process.env.VECTORIZE_ORG!,
authorization: process.env.VECTORIZE_TOKEN!
},
true, // white-label
"My White-Label Google Drive Connector",
"https://api.vectorize.io/v1",
process.env.GOOGLE_OAUTH_CLIENT_ID!,
process.env.GOOGLE_OAUTH_CLIENT_SECRET!
);
Creates a Dropbox OAuth Connector Source via the Vectorize API.
async function createVectorizeDropboxConnector(
config: VectorizeAPIConfig,
connectorName: string,
platformUrl: string = "https://api.vectorize.io/v1"
): Promise<string>
Parameters:
config
: AVectorizeAPIConfig
object containing:organizationId
: Your Vectorize organization IDauthorization
: Your Vectorize API key
connectorName
: Name for the connectorplatformUrl
(optional): URL of the Vectorize API (defaults to 'https://api.vectorize.io/v1')
Returns:
- A
Promise
that resolves with the connector ID
Example:
// Create a Vectorize-managed connector
const connectorId = await createVectorizeDropboxConnector(
{
organizationId: process.env.VECTORIZE_ORG!,
authorization: process.env.VECTORIZE_TOKEN!
},
"My Dropbox Connector",
"https://api.vectorize.io/v1"
);
Creates a White Label Dropbox OAuth Connector Source via the Vectorize API.
async function createWhiteLabelDropboxConnector(
config: VectorizeAPIConfig,
connectorName: string,
appKey: string,
appSecret: string,
platformUrl: string = "https://api.vectorize.io/v1"
): Promise<string>
Parameters:
config
: AVectorizeAPIConfig
object containing:organizationId
: Your Vectorize organization IDauthorization
: Your Vectorize API key
connectorName
: Name for the connectorappKey
: Your Dropbox App keyappSecret
: Your Dropbox App secretplatformUrl
(optional): URL of the Vectorize API (defaults to 'https://api.vectorize.io/v1')
Returns:
- A
Promise
that resolves with the connector ID
Example:
// Create a white-label connector
const connectorId = await createWhiteLabelDropboxConnector(
{
organizationId: process.env.VECTORIZE_ORG!,
authorization: process.env.VECTORIZE_TOKEN!
},
"My White-Label Dropbox Connector",
process.env.DROPBOX_APP_KEY!,
process.env.DROPBOX_APP_SECRET!
);
Manages a Google Drive user for a connector, allowing you to add, edit, or remove users.
async function manageGDriveUser(
config: VectorizeAPIConfig,
connectorId: string,
selectedFiles: Record<string, { name: string; mimeType: string }> | null,
refreshToken: string,
userId: string,
action: "add" | "edit" | "remove",
platformUrl: string = "https://api.vectorize.io/v1"
): Promise<Response>
Parameters:
config
: AVectorizeAPIConfig
objectconnectorId
: ID of the connectorselectedFiles
: Record of selected files with their metadata (name and mimeType)refreshToken
: Google OAuth refresh tokenuserId
: User ID to manageaction
: Action to perform ("add", "edit", or "remove")platformUrl
(optional): URL of the Vectorize API (primarily used for testing)
Returns:
- A
Promise
that resolves with the API response
Example:
// Add a user to a connector
const response = await manageGDriveUser(
{
organizationId: process.env.VECTORIZE_ORG!,
authorization: process.env.VECTORIZE_TOKEN!
},
connectorId,
{
'file-id-1': { name: 'Document 1', mimeType: 'application/pdf' },
'file-id-2': { name: 'Spreadsheet 1', mimeType: 'application/vnd.google-apps.spreadsheet' }
},
refreshToken,
"user123",
"add",
"https://api.vectorize.io/v1"
);
// Edit a user's selected files
const updateResponse = await manageGDriveUser(
{
organizationId: process.env.VECTORIZE_ORG!,
authorization: process.env.VECTORIZE_TOKEN!
},
connectorId,
{
'file-id-3': { name: 'Document 2', mimeType: 'application/pdf' },
'file-id-4': { name: 'Presentation 1', mimeType: 'application/vnd.google-apps.presentation' }
},
refreshToken,
"user123",
"edit",
"https://api.vectorize.io/v1"
);
// Remove a user
const removeResponse = await manageGDriveUser(
{
organizationId: process.env.VECTORIZE_ORG!,
authorization: process.env.VECTORIZE_TOKEN!
},
connectorId,
null, // No files needed for removal
refreshToken,
"user123",
"remove",
"https://api.vectorize.io/v1"
);
Manages a Dropbox user for a connector, allowing you to add, edit, or remove users.
async function manageDropboxUser(
config: VectorizeAPIConfig,
connectorId: string,
selectedFiles: Record<string, { name: string; mimeType: string; path?: string }> | null,
refreshToken: string,
userId: string,
action: "add" | "edit" | "remove",
platformUrl: string = "https://api.vectorize.io/v1"
): Promise<Response>
Parameters:
config
: AVectorizeAPIConfig
objectconnectorId
: ID of the connectorselectedFiles
: Record of selected files with their metadata (name, mimeType, and path)refreshToken
: Dropbox OAuth refresh tokenuserId
: User ID to manageaction
: Action to perform ("add", "edit", or "remove")platformUrl
(optional): URL of the Vectorize API (primarily used for testing)
Returns:
- A
Promise
that resolves with the API response
Example:
// Add a user to a connector
const response = await manageDropboxUser(
{
organizationId: process.env.VECTORIZE_ORG!,
authorization: process.env.VECTORIZE_TOKEN!
},
connectorId,
{
'file-id-1': { name: 'Document 1', mimeType: 'application/pdf', path: '/path/to/doc.pdf' },
'file-id-2': { name: 'Spreadsheet 1', mimeType: 'text/csv', path: '/path/to/data.csv' }
},
refreshToken,
"user123",
"add"
);
// Edit a user's selected files
const updateResponse = await manageDropboxUser(
{
organizationId: process.env.VECTORIZE_ORG!,
authorization: process.env.VECTORIZE_TOKEN!
},
connectorId,
{
'file-id-3': { name: 'Document 2', mimeType: 'application/pdf', path: '/path/to/doc2.pdf' },
'file-id-4': { name: 'Presentation 1', mimeType: 'application/vnd.openxmlformats-officedocument.presentationml.presentation', path: '/path/to/presentation.pptx' }
},
refreshToken,
"user123",
"edit"
);
// Remove a user
const removeResponse = await manageDropboxUser(
{
organizationId: process.env.VECTORIZE_ORG!,
authorization: process.env.VECTORIZE_TOKEN!
},
connectorId,
null, // No files needed for removal
refreshToken,
"user123",
"remove"
);
Gets a one-time authentication token for connector operations. This token is used for secure authentication when redirecting users to the Vectorize platform.
// This function is used server-side in your API endpoint
async function getOneTimeConnectorToken(
config: VectorizeAPIConfig,
userId: string,
connectorId: string,
platformUrl: string = "https://api.vectorize.io/v1"
): Promise<{ token: string; expires_at: number; ttl: number }>
Parameters:
config
: AVectorizeAPIConfig
object containing:authorization
: Your Vectorize authorization tokenorganizationId
: Your Vectorize organization ID
userId
: User ID to include in the tokenconnectorId
: Connector ID to include in the tokenplatformUrl
(optional): URL of the Vectorize API (defaults to 'https://api.vectorize.io/v1')
Returns:
- A
Promise
that resolves with an object containing:token
: The one-time token stringexpires_at
: Timestamp when the token expiresttl
: Time-to-live in seconds
Example:
// Server-side API endpoint implementation (Next.js)
// File: app/api/get-one-time-connector-token/route.ts
import { getOneTimeConnectorToken, VectorizeAPIConfig } from "@vectorize-io/vectorize-connect";
import { NextRequest, NextResponse } from "next/server";
export async function GET(request: NextRequest) {
try {
// Get authentication details from environment variables
const apiKey = process.env.VECTORIZE_TOKEN;
const organizationId = process.env.VECTORIZE_ORG;
if (!apiKey || !organizationId) {
return NextResponse.json({
error: 'Missing Vectorize API configuration'
}, { status: 500 });
}
// Configure the Vectorize API client
const config: VectorizeAPIConfig = {
authorization: apiKey,
organizationId: organizationId
};
// Get userId and connectorId from request url
const { searchParams } = new URL(request.url);
const userId = searchParams.get('userId');
const connectorId = searchParams.get('connectorId');
// Validate userId and connectorId
if (!userId || !connectorId) {
return NextResponse.json({
error: 'Missing userId or connectorId'
}, { status: 400 });
}
// Call Vectorize API to get the token
// This is where we use the SDK function server-side
const tokenResponse = await getOneTimeConnectorToken(
config,
userId,
connectorId
);
// Return the token to the client
return NextResponse.json(tokenResponse, { status: 200 });
} catch (error) {
console.error('Error generating token:', error);
return NextResponse.json({
error: 'Failed to generate token',
message: error instanceof Error ? error.message : 'Unknown error'
}, { status: 500 });
}
}
// Client-side usage
// In your component or page:
const getConnectorToken = async (userId, connectorId) => {
try {
const response = await fetch(`/api/get-one-time-connector-token?userId=${userId}&connectorId=${connectorId}`);
if (!response.ok) {
throw new Error(`Failed to generate token. Status: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error('Error getting connector token:', error);
throw error;
}
};
// Example usage
const tokenResponse = await getConnectorToken('user123', 'connector-id');
console.log('One-time token:', tokenResponse.token);
console.log('Token expires at:', new Date(tokenResponse.expires_at).toISOString());
Refreshes an OAuth access token using a refresh token.
async function refreshGDriveAccessToken(
clientId: string,
clientSecret: string,
refreshToken: string
): Promise<OAuthResponse>
Parameters:
clientId
: The OAuth client IDclientSecret
: The OAuth client secretrefreshToken
: The refresh token
Returns:
- A
Promise
resolving to anOAuthResponse
with a new access token
Example:
const tokens = await refreshGDriveAccessToken(
process.env.GOOGLE_OAUTH_CLIENT_ID!,
process.env.GOOGLE_OAUTH_CLIENT_SECRET!,
storedRefreshToken
);
console.log('New access token:', tokens.access_token);
Exchanges an authorization code for OAuth tokens.
async function exchangeGDriveCodeForTokens(
code: string,
clientId: string,
clientSecret: string,
redirectUri: string
): Promise<OAuthResponse>
Parameters:
code
: The authorization code from OAuth redirectclientId
: The OAuth client IDclientSecret
: The OAuth client secretredirectUri
: The redirect URI used in the authorization
Returns:
- A
Promise
resolving to anOAuthResponse
containing access and refresh tokens
Example:
const tokens = await exchangeGDriveCodeForTokens(
authCode,
process.env.GOOGLE_OAUTH_CLIENT_ID!,
process.env.GOOGLE_OAUTH_CLIENT_SECRET!,
`${process.env.NEXT_PUBLIC_BASE_URL}/api/google-callback`
);
console.log('Access token:', tokens.access_token);
console.log('Refresh token:', tokens.refresh_token);
Refreshes an OAuth access token using a refresh token.
async function refreshDropboxToken(
refreshToken: string,
appKey: string,
appSecret: string
): Promise<{
access_token: string;
expires_in: number;
token_type: string;
}>
Parameters:
refreshToken
: The refresh tokenappKey
: The Dropbox App keyappSecret
: The Dropbox App secret
Returns:
- A
Promise
resolving to an object with a new access token
Example:
const tokens = await refreshDropboxToken(
storedRefreshToken,
process.env.DROPBOX_APP_KEY!,
process.env.DROPBOX_APP_SECRET!
);
console.log('New access token:', tokens.access_token);
Exchanges an authorization code for OAuth tokens.
async function exchangeDropboxCodeForTokens(
code: string,
appKey: string,
appSecret: string,
redirectUri: string
): Promise<OAuthResponse>
Parameters:
code
: The authorization code from OAuth redirectappKey
: The Dropbox App keyappSecret
: The Dropbox App secretredirectUri
: The redirect URI used in the authorization
Returns:
- A
Promise
resolving to anOAuthResponse
containing access and refresh tokens
Example:
const tokens = await exchangeDropboxCodeForTokens(
authCode,
process.env.DROPBOX_APP_KEY!,
process.env.DROPBOX_APP_SECRET!,
`${process.env.NEXT_PUBLIC_BASE_URL}/api/dropbox-callback`
);
console.log('Access token:', tokens.access_token);
console.log('Refresh token:', tokens.refresh_token);
A module for Google Drive picker functionality.
const GoogleDrivePicker = {
createPickerHTML(tokens: OAuthResponse, config: any, refreshToken: string): string
}
Methods:
createPickerHTML
: Creates an HTML template for the picker pagetokens
: OAuth tokens for API accessconfig
: Configuration with API key and client IDrefreshToken
: Refresh token to include in selection data- Returns: HTML string for the picker interface
Example:
// This is typically used internally by the SDK
const htmlContent = GoogleDrivePicker.createPickerHTML(
tokens,
{
clientId: process.env.GOOGLE_OAUTH_CLIENT_ID!,
apiKey: process.env.GOOGLE_API_KEY!
},
refreshToken
);
Note: The GoogleDrivePicker
component is primarily used internally by the SDK and you typically won't need to use it directly.
A module for Dropbox picker functionality.
const DropboxPicker = {
createPickerHTML(tokens: OAuthResponse, config: any, refreshToken: string, preSelectedFiles?: Record<string, { name: string; mimeType: string; path?: string }>): string
}
Methods:
createPickerHTML
: Creates an HTML template for the picker pagetokens
: OAuth tokens for API accessconfig
: Configuration with App key and App secretrefreshToken
: Refresh token to include in selection datapreSelectedFiles
: Optional map of files to initialize as selected- Returns: HTML string for the picker interface
Example:
// This is typically used internally by the SDK
const htmlContent = DropboxPicker.createPickerHTML(
tokens,
{
appKey: process.env.DROPBOX_APP_KEY!,
appSecret: process.env.DROPBOX_APP_SECRET!
},
refreshToken,
preSelectedFiles
);
Note: The DropboxPicker
component is primarily used internally by the SDK and you typically won't need to use it directly.