Integrations

Node & Express

@hopfield/core is an isomorphic client: the same package that powers the browser SDKs runs in any Node 18+ runtime. Use it to search from your server, proxy queries, or index content programmatically.

Install

npm install @hopfield/core

Search from the server

search.ts
import { HopfieldClient } from "@hopfield/core";

const hopfield = new HopfieldClient({
  publicApiKey: process.env.HOPFIELD_PUBLIC_KEY!,
});

const { hits } = await hopfield.search({
  q: "how do refunds work",
  limit: 10,
});

Express: a search proxy route

Proxying through your backend lets you add rate limiting, caching, or auth in front of search without exposing any key to the client:

server.ts
import express from "express";
import { HopfieldClient } from "@hopfield/core";

const app = express();
const hopfield = new HopfieldClient({
  publicApiKey: process.env.HOPFIELD_PUBLIC_KEY!,
});

app.get("/search", async (req, res) => {
  const q = String(req.query.q ?? "").trim();
  if (!q) return res.json({ hits: [] });

  try {
    const result = await hopfield.search({ q, limit: 10 });
    res.json(result);
  } catch (err) {
    res.status(502).json({ error: "search unavailable" });
  }
});

app.listen(3000);

Index content with the admin client

Construct the client with your adminToken (never in browser code) to write to the index:

index-content.ts
import { HopfieldClient } from "@hopfield/core";

const hopfield = new HopfieldClient({
  adminToken: process.env.HOPFIELD_ADMIN_TOKEN,
});

// Index a manual record (e.g. from your database)
await hopfield.indexDocument("YOUR_COLLECTION_ID", {
  title: "Winter sale: up to 40% off",
  content: "Seasonal discounts on all outdoor gear until Jan 31.",
  url: "https://your-site.com/sale",
  description: "Save on jackets, boots, and more.",
});

// Fetch and index a page by URL
await hopfield.indexPage(
  "YOUR_COLLECTION_ID",
  "https://your-site.com/blog/new-post",
);

// Remove a record
await hopfield.deleteDocument("YOUR_COLLECTION_ID", 42);

Related

Full request/response shapes are in the REST API and indexing API references.