-
Notifications
You must be signed in to change notification settings - Fork 0
Getting started with the JS SDK
The JS SDK is the foundation to:
- Build Gally connectors that will interact with Gally using Javascript (using Medusa JS, Strapi, etc...)
- Implement front-end features that uses Gally (autocomplete, user behavior analytics, etc...)
Using the Gally JS SDK you will be able to interact with your catalog using Javascript on both client and server side.
The SDK can help you do the following operations:
- Catalog Management: syncing catalogs, metadata, source fields, and field options
- Indexation: Creating, bulk indexing, and managing search indexes
- Search: Full-text search, filtering, faceting, sorting, and pagination
- Track user interactions: views, searches, add-to-cart, orders
In order to use the SDK you will need
- Node.js >= 18.0.0 (uses native fetch)
- NPM (if your application uses a bundler)
npm install @elastic-suite/gally-sdk
The Gally SDK requires configuration to connect to your Gally instance.
import { Configuration } from '@gally/sdk'
const config = new Configuration({
baseUri: 'https://your-gally-instance.com/api/',
user: 'admin@example.com',
password: 'your-password',
checkSSL: true,
})| Option | Type | Required | Default | Description |
|---|---|---|---|---|
baseUri |
string |
✅ | - | Base URI of your Gally API (must end with /) |
user |
string |
- | User email for authentication (not required for browser/public APIs) | |
password |
string |
- | User password (not required for browser/public APIs) | |
checkSSL |
boolean |
❌ | true |
Enable SSL certificate verification |
Note: user and password are only required for backend operations (catalog sync, indexing). For browser usage (search and tracking), credentials are not needed as these are public APIs.
For client-side applications, for easier usage, you can pass the config object or juste the baseUri directly.
import { SearchManager, TrackingEventManager, Configuration } from '@gally/sdk/browser'
// Use for search
const searchManager = new SearchManager({ baseUri })
// Use for tracking
const trackingManager = new TrackingEventManager({ baseUri })Search and tracking are public-facing APIs designed to be called directly from the browser:
- ✅ Search - Users need to search your catalog
- ✅ Tracking - Track user behavior for analytics
Authentication is only required for administrative operations:
⚠️ Catalog sync - Modifying catalog structure⚠️ Indexing - Managing search indexes
When using the SDK in the browser:
- ✅ Use HTTPS - Always use secure connections
- ✅ Rate limiting - Implement client-side rate limiting
- ✅ CORS - Ensure your Gally instance allows your domain
- ❌ Never expose admin credentials - Use the browser bundle only for search and tracking
See environments specific configurations
const devConfig = new Configuration({
baseUri: 'http://localhost:8000/api/',
user: 'dev@example.com',
password: 'dev-password',
checkSSL: false, // Disable SSL check for local development
})const prodConfig = new Configuration({
baseUri: 'https://gally.yourcompany.com/api/',
user: process.env.GALLY_USER,
password: process.env.GALLY_PASSWORD,
checkSSL: true, // Always enable SSL check in production
})Store sensitive configuration in environment variables:
# .env
GALLY_BASE_URI=https://your-gally-instance.com/api/
GALLY_USER=admin@example.com
GALLY_PASSWORD=your-secure-password
GALLY_CHECK_SSL=trueLoad with dotenv:
import 'dotenv/config'
const config = new Configuration({
baseUri: process.env.GALLY_BASE_URI!,
user: process.env.GALLY_USER!,
password: process.env.GALLY_PASSWORD!,
checkSSL: process.env.GALLY_CHECK_SSL === 'true',
})You can implement custom token caching to improve performance and reduce authentication requests
See example of token cache manager implementations
import { TokenCacheManager } from '@gally/sdk'
// Example: In-memory cache
const tokenCache: TokenCacheManager = {
async getToken(getToken, useCache = true) {
if (useCache) {
const cached = await myCache.get('gally_token')
if (cached) return cached
}
const token = await getToken()
await myCache.set('gally_token', token)
return token
},
}
// Use with services
const indexOp = new IndexOperation(config, tokenCache)
const trackingManager = new TrackingEventManager(config, tokenCache)import Redis from 'ioredis'
const redis = new Redis()
const tokenCache: TokenCacheManager = {
async getToken(getToken, useCache = true) {
const cacheKey = 'gally:auth:token'
if (useCache) {
const cached = await redis.get(cacheKey)
if (cached) {
return JSON.parse(cached)
}
}
const token = await getToken()
// Cache for 1 hour (tokens typically expire after 1 hour)
await redis.setex(cacheKey, 3600, JSON.stringify(token))
return token
},
}The SDK validates configuration on initialization:
try {
const config = new Configuration({
baseUri: 'invalid-url', // ❌ Missing protocol
user: '', // ❌ Empty user
password: '', // ❌ Empty password
})
} catch (error) {
console.error('Configuration error:', error.message)
}- Use environment variables for sensitive data
- Enable SSL verification in production
- Implement token caching to reduce authentication overhead
- Validate configuration at application startup
- Use separate configurations for different environments
- Rotate credentials regularly for security
- Never commit credentials to version control
The SDK is designed to be as isomorphic as possible so you can use it on both server and client side. But some operations are better done on one side by design.
The provides a full export for Node and reduced export for browser specific functions
Catalog management and indexation are typically done on server side because they are heavy operation that will use your database. The Node bundle theoretically allows access to the full SDK, but using the tracking feature on node side is not recommended as it is not guaranteed to work in the future
import { StructureSynchronizer, IndexOperation } from '@gally/sdk'Tracking will be done on client side most of the time as it relies on real time user interactions.
Searching is also included in the browser bundle as SearchManager handles autocompletion, facets etc...
import { SearchManager, TrackingEventManager } from '@gally/sdk/browser'Search can be done on both depending on your needs
Now you can start to manage your content with the JS SDK
1. Getting started
2. Managing your content
3. Searching and tracking user interactions