Skip to main content
This page is the framework-and-provider cookbook: drop-in snippets for FastAPI, Flask, Next.js, Django, OpenAI, Anthropic, Google Gemini (free tier), and the Vercel AI SDK. It assumes you’ve already finished the Quickstart and have an API key.If you want a fully-worked walkthrough that builds one application end-to-end with conversation routing, error handling, and graceful degradation, follow First Integration instead, then come back here when you need to swap frameworks or LLM providers.

Overview

This guide walks you through integrating Synap into your application. You will learn how to initialize the SDK in popular Python web frameworks, bridge async/sync execution models, and wire Synap into your LLM provider’s generation pipeline. By the end of this page, your application will have a working memory-augmented agent pattern: retrieve context from Synap, inject it into the LLM prompt, generate a response, and ingest the conversation back into Synap.

Framework Integration

FastAPI is the most common framework for Synap integrations. Use the lifespan event for initialization and shutdown, and access the SDK instance from your route handlers.
Storing the SDK on app.state (instead of a module-level global) keeps it scoped to the FastAPI app instance, which makes integration tests trivial: TestClient(app) creates a fresh app with its own SDK, and you can replace app.state.sdk with a mock before each test. The Depends(get_sdk) indirection then lets you override the SDK per-test via app.dependency_overrides[get_sdk] = lambda: mock_sdk.

Async/Sync Bridging

The Synap SDK is async-native. If your application uses a synchronous framework, you need to bridge the async calls. Here are the recommended patterns:
Use for scripts, CLI tools, and simple synchronous applications. Creates a new event loop for each call.
asyncio.run() creates a new event loop each time. This is fine for low-frequency calls but adds overhead for high-throughput applications.
Create a single event loop at startup and reuse it for all SDK calls. This is the pattern shown in the Flask and Django examples above.
If you are using Django’s async views alongside sync code, asgiref provides utilities for bridging:

LLM Provider Integration

Full example of a memory-augmented agent using OpenAI’s gpt-4o:

The Memory-Augmented Agent Pattern

Every Synap integration follows the same fundamental pattern:
1

Retrieve

Fetch relevant memories from Synap based on the user’s message, identity, and conversation history.
2

Build Prompt

Inject retrieved memories into the system prompt or message history. This gives the LLM access to personalized, contextual information.
3

Generate

Call your LLM provider with the enriched prompt. The model generates a response informed by the user’s history, preferences, and organizational context.
4

Ingest

Send the conversation turn back to Synap for processing and long-term storage. This closes the loop: today’s conversation becomes tomorrow’s context.

Error Handling and Graceful Degradation

Synap should enhance your application, not be a single point of failure. Design your integration to degrade gracefully when Synap is unavailable.
Always wrap Synap SDK calls in try/except blocks in production. Network issues, rate limits, or service disruptions should not prevent your application from responding to users. The LLM can still generate useful responses without memory context; it just won’t be personalized.

Next Steps

Authentication

Configure API keys for secure communication.

SDK Initialization

Detailed SDK configuration options.

First Integration Guide

Step-by-step tutorial for your first Synap integration.