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

# SDK

> Install and use the memex-sdk package for the public Memex REST API

Use `memex-sdk` when you want a typed npm client for the public Memex REST API instead of calling the endpoints manually.

> [!WARNING]
>
> `memex-sdk` is currently in beta. The SDK surface and API response shapes
> will likely change as the Memex API evolves.

## Install

```bash theme={null}
npm install memex-sdk
```

## What it wraps

The SDK currently wraps the public REST endpoints documented on:

* [Available endpoints](https://docs.memex.garden/general/available-endpoints.md)
* [Response shape](https://docs.memex.garden/general/response-shape.md)
* [Authentication](https://docs.memex.garden/general/authentication.md)

Included client methods:

* `search`
* `searchContent`
* `saveContentEntity`
* `saveContentByUrl`
* `getAvailablePlans`
* `getAvailableTagNames`
* `agentCreateUser`

Use `saveContentEntity` for new URL saves:

```ts theme={null}
await client.saveContentEntity({
    data: {
        url: 'https://example.com/article',
    },
    tags: ['research'],
})
```

Use `saveContentByUrl` when you have a flat URL payload.

## Basic usage

```ts theme={null}
import { MemexClient } from 'memex-sdk'

const client = new MemexClient({
    apiKey: process.env.MEMEX_API_KEY,
})

const searchResult = await client.search({
    query: 'vector databases',
    limit: 5,
})
```

## Search modes

The SDK follows the same search contract as the REST API:

* omit `raw` or pass `raw: false` to get the default compact response with a `results` array of slim result objects
* pass `raw: true` to get the richer machine-readable response
* if a wrapper asks for `llm` or `full`, choose `llm` for `raw: false` and `full` for `raw: true`
* compact `raw=false` search defaults reranking on for eligible queries
* raw `raw=true` search defaults reranking off unless you set `enableRerank: true`

```ts theme={null}
const llmReady = await client.search({
    query: 'context mode',
})

const raw = await client.search({
    query: 'context mode',
    raw: true,
    includeNotes: true,
})

const llmReadyViewSearch = await client.search({
    viewIds: ['view_id'],
    query: 'vector database benchmarks',
    raw: false,
})

const fullViewSearch = await client.search({
    viewIds: ['view_id'],
    query: 'vector database benchmarks',
    raw: true,
    includeNotes: true,
})
```

For the full shape details, including compact `user_notes`/`media` fields and raw `referencesByResultId`, see [Response shape](https://docs.memex.garden/general/response-shape.md).

## Authentication

For most SDK calls, use either:

* `apiKey`, which the SDK sends as `X-Api-Key: <apiKey>`
* `bearerToken`, which the SDK sends as `Authorization: Bearer <token>`

```ts theme={null}
const client = new MemexClient({
    bearerToken: process.env.MEMEX_BEARER_TOKEN,
})
```

`agentCreateUser` is different. It requires `x-agent-service-token`:

```ts theme={null}
const created = await client.agentCreateUser(
    {
        targetUserEmail: 'user@example.com',
    },
    {
        agentServiceToken: process.env.MEMEX_AGENT_SERVICE_TOKEN!,
    },
)
```

## Error handling

The SDK throws `MemexApiError` for non-2xx responses and preserves the parsed response body when possible.

```ts theme={null}
import { MemexApiError } from 'memex-sdk'

try {
    await client.search({
        query: 'vector databases',
    })
} catch (error) {
    if (error instanceof MemexApiError) {
        console.error(error.status, error.errorCode, error.responseBody)
    }
}
```

## Related docs

* [Available endpoints](https://docs.memex.garden/general/available-endpoints.md)
* [Response shape](https://docs.memex.garden/general/response-shape.md)
* [Authentication](https://docs.memex.garden/general/authentication.md)
* [Buy credits](https://docs.memex.garden/general/buy-credits.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.