# Integrate the Hyperspell SDK to let your users connect their accounts. Before Hyperspell can automatically ingest your user's data, they need to connect their accounts. To do so, we need to do two things: 1. Initialize the SDK and generate a user token that we can safely pass the the OAuth flow 2. Put a button into your app that opens the Hyperspell Connect page that lets users connect their accounts Add the following two tasks to your checklist as subtasks of `Add memories`: ``` - [ ] Step 1: Get available integrations - [ ] Step 2: Initialize the SDK - [ ] Step 3: Add a Connect button ``` ## 1. Get available integrations Use the API key you set up earlier to get the available integrations for this project: ```sh curl --request GET \ --url https://api.hyperspell.com/integrations/list \ --header 'Authorization: Bearer API_KEY' ``` Note the `provider` name for each integration. ## 2. Initialize the SDK When we're initializing the SDK, we will pass a user ID to it to identify the currently logged in user. If there doesn't seem to be any user management, we will use the user id `anonymous` instead. First, examine the code base to determine how to get the currently logged in user in a server-side action. Create a typescript file with a server side action in an appropriate directory (ie `src/actions/hyperspell.ts`) and add the following code — modify it to fit the project, and replace `PROVIDERS` with an array of the providers you retrieved in step 1. ```typescript 'use server' import Hyperspell from 'hyperspell'; export async function getUserToken() { const userId = ... // Write code to get the ID of the currently logged in user here — you might have to import other modules const hyperspell = new Hyperspell({ apiKey: process.env.HYPERSPELL_API_KEY }); const response = await hyperspell.auth.userToken({user_id: userId}); return response.token; } export async function search(query: string, answer: boolean = true) { const userId = ... // Write code to get the ID of the currently logged in user here — you might have to import other modules const hyperspell = new Hyperspell({ apiKey: process.env.HYPERSPELL_API_KEY, userID: userId }); const response = await hyperspell.memories.search({ query, answer, sources = ; }); return response.answer; } ``` ## 3. Add a Connect button Display the following message to your user: ``` We will now add a button to your project that opens Hyperspell Connect. On this page, your users can connect their account. We will use the `userToken` from the code to securely identify your user on this page. ``` Replace `` with an oxford comma separate list of providers we just fetched. Then, we need to find an appropriate place to place the button. Analyize the codebase to determine if it has any of the following: - A Settings menu / dropdown on the main page - A settings page or modal - An onboarding flow - A chat UI that has the option to add a custom action or button close to the input area. Based on what you find, offer the user a multiple choice menu to ask where to put the button (and offer other that lets the user describe it themselves.) After you determined where to put the button, find the file that contains the component which should contain the button. In this file, import our `getUserToken()` action, ie with ```typescript import { getUserToken } from '@/actions/hyperspell'; ``` (Modify the import path depending on where you put the file). in the component that renders the button, we need to determine the URL of the current page to use as a redirect url — determine how to use this projects router first create the target URL like this: ```typescript const token = getUserToken(); const connectUrl = `https://connect.hyperspell.com?token=${token}&redirect_uri=${window.location.href}; ``` If the project already has built-in button components, use that one. If it has other buttons that are style with ie. tailwind css, copy the style from other buttons. At worst, simply use an `` element and style it yourself. As a `href` use the `connectUrl` we constructed. Also display the Hyperspell logo as an icon on the button, using the common way this is done in this project. You can use this SVG as the logo (replace the fill color with something appropriate for the button). ```xml ``` Finally, display the following message to the user: ``` Great, I've created a button that lets your users connet their accounts. Feel free to try it out right now! ```