> ## Documentation Index
> Fetch the complete documentation index at: https://docs.withfoundry.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Write memories

> Write single or batch entries to the Memory Engine with optional metadata.

# Write memories

Write text entries into a namespace. Each entry is embedded and indexed for semantic search. You can attach optional `metadata` for filtering or display.

## SDK

```javascript theme={null}
import { Foundry } from '@withfoundry/sdk';

const foundry = new Foundry({ apiKey: process.env.FOUNDRY_API_KEY });

// Single entry
const { data } = await foundry.memory.write({
  namespace: 'my-app',
  entries: [
    {
      content: 'User preferences: theme=dark, timezone=UTC.',
      metadata: { type: 'preferences', userId: 'u_123' },
    },
  ],
});
// data.ids = ['mem_abc123...']
```

## Batch write

Send multiple entries in one request:

```javascript theme={null}
const { data } = await foundry.memory.write({
  namespace: 'my-app',
  entries: [
    { content: 'First fact.', metadata: { source: 'doc' } },
    { content: 'Second fact.', metadata: { source: 'doc' } },
    { content: 'Third fact.', metadata: { source: 'chat' } },
  ],
});
// data.ids = ['mem_...', 'mem_...', 'mem_...']
```

## REST API

`POST /v1/memory/entries`

**Headers:** `Authorization: Bearer key_...`\
**Body:**

```json theme={null}
{
  "namespace": "my-app",
  "entries": [
    {
      "content": "Your text here.",
      "metadata": { "key": "value" }
    }
  ]
}
```

**Response (envelope):**

```json theme={null}
{
  "success": true,
  "data": {
    "ids": ["mem_abc123", "mem_def456"]
  },
  "meta": { "requestId": "req_xyz" }
}
```

## Metadata

* **Optional.** Use for filtering (e.g. `metadata.source`, `metadata.userId`) or for display in your UI.
* Stored as JSON; avoid huge objects to keep responses and indexes lean.

## Idempotency and updates

* Each write creates new entries and new IDs. To “update,” write a new entry and optionally delete or deprecate the old one via your own logic or future delete API.
* For idempotent writes, include a stable `metadata.id` and deduplicate in your app before calling write.

Next: [Search](/memory/search) to query these entries by meaning.
