Backend Setup
Before shipping your game, you must route NPC requests through your backend. This page explains why and how to configure the SDK.
Why You Need a Backend
During development, the SDK calls LoreMind directly using your Server API Key stored in EditorPrefs. This is convenient for prototyping but not safe for production.
The Problem with Direct Calls
If you ship a game that calls LoreMind directly:
- Your API key is in the build - Anyone can extract it
- No player identification - Can’t use long-term memory
- No custom rate limiting - Can’t control per-player usage
- No audit trail - Can’t track who’s making requests
The Backend Pattern
Instead, your game calls your backend, and your backend calls LoreMind:
┌─────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Your Game │ --> │ Your Backend │ --> │ LoreMind API │
│ (Client) │ │ (adds API key) │ │ │
└─────────────┘ └─────────────────┘ └─────────────────┘Your backend:
- Authenticates players - Using Steam, Epic, PlayFab, or custom auth
- Adds the API key - Stored securely in environment variables
- Adds player ID - Enables long-term memory
- Forwards to LoreMind - And returns responses to your game
Before You Start
You need:
- A working NPC in the Editor - Complete Installation and Your First NPC first
- A game backend - PlayFab, Firebase, Nakama, or custom server
- Backend code that calls LoreMind - See Server Integration for examples
Configuring the SDK
Step 1: Set Up Your Backend Endpoint
Your backend needs an endpoint that:
- Receives NPC requests from your game
- Authenticates the player
- Adds your LoreMind API key
- Forwards to
https://loremind.peekgames.dev/api/loremind/v1/npc/interact - Returns the response
See Server Integration for complete backend examples (PlayFab, Firebase, Nakama, Node.js, .NET).
Step 2: Configure the SDK
- Open Window > LoreMind > Control Panel
- Set Transport Mode to Custom Backend
- Enter your Backend URL (e.g.,
https://api.yourgame.com/api/npc/interact) - Click Save
The SDK will now route all NPC requests to your backend instead of calling LoreMind directly.
Step 3: Test in a Build
- Create a development build
- Run it and test NPC interactions
- Verify requests go through your backend (check server logs)
- Verify responses work correctly
What Your Backend Receives
The SDK sends this JSON to your backend:
{
"text": "Hello, what's your name?",
"entityMindId": "em_innkeeper_garrick",
"context": {
"location": "The Rusty Anvil Tavern",
"timeOfDay": "evening",
"nearbyCharacters": ["Mysterious stranger"]
},
"memory": {
"retrieve": true
}
}Your backend should:
- Authenticate the player using your auth system
- Add
playerIdfrom your auth (e.g., Steam ID, account ID) - Add
Authorization: Bearer YOUR_API_KEYheader - Forward to LoreMind
- Return the response to the game
Programmatic Configuration
You can also configure the transport in code:
using Peek.LoreMind;
// Set the backend URL programmatically
var transport = new CustomBackendTransport(
backendUrl: "https://api.yourgame.com/api/npc/interact"
);
npc.SetTransport(transport);This is useful if you need different backends for different environments (dev, staging, production).
Long-Term Memory
Memory requires playerId to identify players. Your backend provides this:
// Your backend adds this before forwarding to LoreMind
{
"text": "Hello!",
"entityMindId": "em_innkeeper",
"playerId": "steam_76561198012345678", // Your backend adds this
"memory": { "retrieve": true }
}Good player ID choices:
- Platform IDs:
steam_76561198012345678,epic_abc123 - Game account IDs:
account_12345 - Persistent UUIDs:
550e8400-e29b-41d4-a716-446655440000
Avoid:
- Temporary session IDs (memories won’t persist)
- Email addresses (PII concerns)
- Device IDs (players switch devices)
See Long-Term Memory for details.
Testing Checklist
Before shipping, verify:
- API key is stored in backend environment variables (not in code)
- API key is NOT in your game build
- Player authentication works correctly
- Requests include
playerId - Error responses are handled gracefully
- Rate limits reviewed in Dashboard
- Long-term memory works (if using)
Troubleshooting
”Unauthorized” errors from LoreMind
- Check your API key is correct (starts with
sk_server_) - Verify the key is in the
Authorizationheader - Make sure you’re using a Server Key, not an Editor Key
”Entity Mind not found”
- Verify the
entityMindIdexists in your dashboard - Check for typos in the ID
Requests not reaching your backend
- Check the backend URL is correct in Control Panel
- Verify your backend is running and accessible
- Check for CORS issues if testing in browser
Memory not working
- Ensure
playerIdis included in requests - Use consistent player IDs across sessions
- Verify
memory: { retrieve: true }is included
Security Best Practices
Never Expose Your API Key
- Store it in environment variables on your backend
- Never commit it to version control
- Never include it in game builds
Validate Requests
- Authenticate players before forwarding requests
- Consider validating
entityMindIdagainst a whitelist
Use HTTPS
- LoreMind API requires HTTPS
- Your backend should require HTTPS too
Monitor Usage
- Set up billing alerts in the dashboard
- Review rate limits in Dashboard → Project Settings
- Track per-player usage if needed
Next Steps
- Server Integration - Backend code examples for all platforms
- Long-Term Memory - Make NPCs remember players
- API Reference - Full API documentation