Sessions

Overview

A session in Fuse allows you to launch the importer by generating a session token. You can store metadata such as user IDs inside of a session so that when you retrieve imports from the Fuse API, you know which user in your system to associate them with.

Depending on your needs, you can choose between two options:

  1. Client-Side Sessions: A simpler method that generates the session token directly in the frontend, though it exposes your API key and should only be used in trusted or testing environments.

  2. Server-Side Sessions: The most secure method, as it prevents exposing sensitive data by handling session token generation on your backend.

Client-Side Session

This option is simpler but less secure, as it involves exposing your API key in the frontend. It should only be used in testing environments or trusted internal tools.

Steps:

  1. Implement getSessionToken: Directly generate the session token using the Fuse client-side token function and pass it to the importer.

Frontend Code Sample

import FuseImporter, { getFuseClientSideToken } from 'fuse-importer';

const importer = new FuseImporter();

importer.getSessionToken = getFuseClientSideToken({
  importer_slug: 'YOUR_IMPORTER_SLUG',
  api_key: 'YOUR_API_KEY'
});

importer.show();

Server-Side Sessions

This is the recommended approach for production environments, as it securely handles session token generation on your backend, keeping sensitive information safe.

Steps

  1. Add an API Endpoint: Create an endpoint on your server that calls the Session API to generate session tokens using an API key from your account.
  2. Implement getSessionToken: In your frontend, implement getSessionToken to request the session token from your server’s API endpoint and use it to launch the importer.

Backend Code Sample

const express = require('express');
const axios = require('axios');
const app = express();
app.use(express.json());

app.post('/api/getSessionToken', async (req, res) => {
  const { importerSlug } = req.body;
  const metadata = { user_id: 123 };

  try {
    const response = await axios.post('https://fuse.flatirons.com/api/v1/importer/sessions', {
      importer_slug: importerSlug,
      metadata: metadata
    }, {
      headers: { 'Authorization': `Bearer YOUR_API_TOKEN` }
    });
    res.json({ token: response.data.token });
  } catch (error) {
    res.status(500).json({ error: 'Failed to get session token' });
  }
});

app.listen(3000, () => console.log('Server running on port 3000'));

Frontend Code Sample

const importer = new FuseImporter();

importer.getSessionToken = async () => {
  const response = await fetch('/api/getSessionToken', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
  });
  const data = await response.json();
  return data.token;
};

importer.show();