Server Integration
This guide shows how to integrate LoreMind with popular backend platforms. Choose the pattern that matches your stack.
Prerequisites
- A Server Key (
sk_server_*) — see Authentication; it lives on your backend, never in game builds - An authenticated source of
playerId— required for rate limiting, memory, and conversation tracking - Your backend platform’s tooling (Nakama, PlayFab, Node.js, or a custom C# service)
Why Use a Backend?
Your backend:
- Keeps API keys secure - Never expose keys in game builds
- Identifies players - Provide
playerId(required) for rate limiting, memory, and conversation tracking - Adds your own logic - Custom rate limiting, validation, logging, analytics
Important: The LoreMind API key must never be in your game client. Only your backend should have it.
See Backend Integration for a simpler getting-started guide.
Integration Patterns
Nakama (TypeScript)
Nakama is an open-source game server supporting realtime multiplayer, matchmaking, and server-side logic. LoreMind integrates via Nakama’s TypeScript runtime.
Register the RPC
// main.ts
const InitModule: nkruntime.InitModule = function(
ctx: nkruntime.Context,
logger: nkruntime.Logger,
nk: nkruntime.Nakama,
initializer: nkruntime.Initializer
) {
initializer.registerRpc('npc_interact', rpcNpcInteract);
logger.info('LoreMind NPC module loaded');
};
const LOREMIND_URL = 'https://loremind.peekgames.dev/api/loremind/v1/npc/interact';
const LOREMIND_API_KEY = 'sk_server_your_key_here'; // Placeholder — load from your server's env/config, never hardcodeNPC Interaction RPC
const rpcNpcInteract: nkruntime.RpcFunction = function(
ctx: nkruntime.Context,
logger: nkruntime.Logger,
nk: nkruntime.Nakama,
payload: string
): string {
const request = JSON.parse(payload);
const playerId = ctx.userId; // Nakama user ID from authenticated session
// Client sends context from Unity scene - server just forwards it
const body = JSON.stringify({
text: request.message,
entityMindId: request.entityMindId,
playerId: playerId, // Server provides authenticated player ID
memory: { retrieve: true },
context: request.context // Context comes from client (LocationZone, ContextTag, etc.)
});
// Call LoreMind API
const response = nk.httpRequest(LOREMIND_URL, 'post', {
'Content-Type': 'application/json',
'Authorization': `Bearer ${LOREMIND_API_KEY}`
}, body, 30000);
if (response.code !== 200) {
logger.error(`LoreMind error: ${response.code} ${response.body}`);
throw new Error('NPC service unavailable');
}
const result = JSON.parse(response.body);
logger.info(`NPC response for ${playerId}: ${result.character}`);
return JSON.stringify({
response: result.response,
character: result.character
});
};Unity Client (with Nakama)
The client gathers context from the scene using SDK components (LocationZone, ContextTag, etc.) and sends it to the server:
using Nakama;
using Peek.LoreMind;
public class NpcClient : MonoBehaviour
{
private IClient _client;
private ISession _session;
[SerializeField] private LoreMindNPC npc;
public async Task<string> TalkToNpc(string message)
{
// Client builds context from scene state
var payload = new
{
entityMindId = npc.EntityMindId,
message = message,
context = new
{
location = npc.Context.location,
locationDetails = npc.Context.locationDetails,
timeOfDay = npc.Context.timeOfDay,
weather = npc.Context.weather,
nearbyCharacters = npc.Context.nearbyCharacters,
playerAppearance = npc.Context.playerAppearance,
recentEvents = npc.Context.recentEvents
}
};
var response = await _client.RpcAsync(_session, "npc_interact",
JsonConvert.SerializeObject(payload));
var result = JsonConvert.DeserializeObject<NpcResponse>(response.Payload);
return result.response;
}
}Resources:
Request & Response Format
Whatever the platform, the call to LoreMind is the same: POST /npc/interact with text, entityMindId, and your authenticated playerId — plus optional context and memory.retrieve. Full request and response schemas, every context field, and the complete error table live in the POST /npc/interact reference.
Troubleshooting
401 Invalid API keyor403 Invalid authentication— you’re using an Editor Key or an incomplete key; this endpoint needs the full Server Key (sk_server_*)429 Rate limit exceeded— wait per theRetry-Afterheader; defaults and configuration are in Errors & Rate Limits503 Generation failed— transient LLM error; retry, and show players a graceful fallback line (as the Mirror example does)
The full error reference is Errors & Rate Limits.
Next Steps
- Authentication - Set up API keys
- Long-Term Memory - Player memory across sessions
- Entity Minds - Configure NPC personalities