--- title: Guide description: 'Get started with Mem0 Platform in minutes' --- ## 1. Installation ```bash pip pip install mem0ai ``` ```bash npm npm install mem0ai ``` ## 2. API Key Setup 1. Sign in to [Mem0 Platform](https://mem0.dev/pd-api) 2. Copy your API Key from the dashboard ![Get API Key from Mem0 Platform](/images/platform/api-key.png) ## 3. Instantiate Client ```python Python from mem0 import MemoryClient client = MemoryClient(api_key="your-api-key") ``` ```javascript JavaScript const MemoryClient = require('mem0ai'); const client = new MemoryClient('your-api-key'); ``` ## 4. Memory Operations Mem0 provides a simple and customizable interface for performing CRUD operations on memory. ### 4.1 Create Memories #### Long-term memory for a user These memory instances persist across multiple sessions. Ideal for maintaining memory over long time spans. ```python Python messages = [ {"role": "user", "content": "Hi, I'm Alex. I'm a vegetarian and I'm allergic to nuts."}, {"role": "assistant", "content": "Hello Alex! I've noted that you're a vegetarian and have a nut allergy. I'll keep this in mind for any food-related recommendations or discussions."} ] client.add(messages, user_id="alex") ``` ```javascript JavaScript const messages = [ {"role": "user", "content": "Hi, I'm Alex. I'm a vegetarian and I'm allergic to nuts."}, {"role": "assistant", "content": "Hello Alex! I've noted that you're a vegetarian and have a nut allergy. I'll keep this in mind for any food-related recommendations or discussions."} ]; client.add(messages, { user_id: "alex" }) .then(response => console.log(response)) .catch(error => console.error(error)); ``` ```bash cURL curl -X POST "https://api.mem0.ai/v1/memories/" \ -H "Authorization: Token your-api-key" \ -H "Content-Type: application/json" \ -d '{ "messages": [ {"role": "user", "content": "Hi, I'm Alex. I'm a vegetarian and I'm allergic to nuts."}, {"role": "assistant", "content": "Hello Alex! I've noted that you're a vegetarian and have a nut allergy. I'll keep this in mind for any food-related recommendations or discussions."} ], "user_id": "alex" }' ``` ```json Output {'message': 'ok'} ``` #### Short-term memory for a user session These memory instances persist only for the duration of a user session. Ideal for non-repetitive interactions and managing context windows efficiently. ```python Python messages = [ {"role": "user", "content": "I'm planning a trip to Japan next month."}, {"role": "assistant", "content": "That's exciting, Alex! A trip to Japan next month sounds wonderful. Would you like some recommendations for vegetarian-friendly restaurants in Japan?"}, {"role": "user", "content": "Yes, please! Especially in Tokyo."}, {"role": "assistant", "content": "Great! I'll remember that you're interested in vegetarian restaurants in Tokyo for your upcoming trip. I'll prepare a list for you in our next interaction."} ] client.add(messages, user_id="alex123", session_id="trip-planning-2024") ``` ```javascript JavaScript const messages = [ {"role": "user", "content": "I'm planning a trip to Japan next month."}, {"role": "assistant", "content": "That's exciting, Alex! A trip to Japan next month sounds wonderful. Would you like some recommendations for vegetarian-friendly restaurants in Japan?"}, {"role": "user", "content": "Yes, please! Especially in Tokyo."}, {"role": "assistant", "content": "Great! I'll remember that you're interested in vegetarian restaurants in Tokyo for your upcoming trip. I'll prepare a list for you in our next interaction."} ]; client.add(messages, { user_id: "alex123", session_id: "trip-planning-2024" }) .then(response => console.log(response)) .catch(error => console.error(error)); ``` ```bash cURL curl -X POST "https://api.mem0.ai/v1/memories/" \ -H "Authorization: Token your-api-key" \ -H "Content-Type: application/json" \ -d '{ "messages": [ {"role": "user", "content": "I'm planning a trip to Japan next month."}, {"role": "assistant", "content": "That's exciting, Alex! A trip to Japan next month sounds wonderful. Would you like some recommendations for vegetarian-friendly restaurants in Japan?"}, {"role": "user", "content": "Yes, please! Especially in Tokyo."}, {"role": "assistant", "content": "Great! I'll remember that you're interested in vegetarian restaurants in Tokyo for your upcoming trip. I'll prepare a list for you in our next interaction."} ], "user_id": "alex123", "session_id": "trip-planning-2024" }' ``` ```json Output {'message': 'ok'} ``` #### Long-term memory for agents Add a memory layer for the assistants and agents so that their responses remain consistent across sessions. ```python Python messages = [ {"role": "system", "content": "You are an AI tutor with a personality. Give yourself a name for the user."}, {"role": "assistant", "content": "Understood. I'm an AI tutor with a personality. My name is Alice."} ] client.add(messages, agent_id="ai-tutor") ``` ```javascript JavaScript const messages = [ {"role": "system", "content": "You are an AI tutor with a personality. Give yourself a name for the user."}, {"role": "assistant", "content": "Understood. I'm an AI tutor with a personality. My name is Alice."} ]; client.add(messages, { agent_id: "ai-tutor" }) .then(response => console.log(response)) .catch(error => console.error(error)); ``` ```bash cURL curl -X POST "https://api.mem0.ai/v1/memories/" \ -H "Authorization: Token your-api-key" \ -H "Content-Type: application/json" \ -d '{ "messages": [ {"role": "system", "content": "You are an AI tutor with a personality. Give yourself a name for the user."}, {"role": "assistant", "content": "Understood. I'm an AI tutor with a personality. My name is Alice."} ], "agent_id": "ai-tutor" }' ``` ```json Output {'message': 'ok'} ``` #### Monitor Memories You can monitor memory operations on the platform dashboard: ![Mem0 Platform Activity](/images/platform/activity.png) ### 4.2 Search Memories #### General Memory Search Pass user messages, interactions, and queries into our search method to retrieve relevant memories. ```python Python query = "What should I cook for dinner today?" client.search(query, user_id="alex") ``` ```javascript JavaScript const query = "What should I cook for dinner today?"; client.search(query, { user_id: "alex" }) .then(results => console.log(results)) .catch(error => console.error(error)); ``` ```bash cURL curl -X POST "https://api.mem0.ai/v1/memories/search/" \ -H "Authorization: Token your-api-key" \ -H "Content-Type: application/json" \ -d '{ "query": "What should I cook for dinner today?", "user_id": "alex" }' ``` ```json Output [ { "id": "7f165f7e-b411-4afe-b7e5-35789b72c4a5", "memory": "Vegetarian. Allergic to nuts.", "input": [ { "role": "user", "content": "Hi, I'm Alex. I'm a vegetarian and I'm allergic to nuts." }, { "role": "assistant", "content": "Hello Alex! I've noted that you're a vegetarian and have a nut allergy. I'll keep this in mind for any food-related recommendations or discussions." } ], "user_id": "alex", "hash": "9ee7e1455e84d1dab700ed8749aed75a", "metadata": null, "created_at": "2024-07-20T01:30:36.275141-07:00", "updated_at": "2024-07-20T01:30:36.275172-07:00" } ] ``` #### Search using custom filters Our advanced search allows you to set custom search filters. You can filter by user_id, agent_id, app_id, date, and more. Here you need to define `version` as `v2` in the search method. Example 1: Search using user_id and agent_id filters ```python Python query = "What do you know about me?" filters = { "AND":[ { "user_id":"alex" }, { "agent_id":{ "in":[ "travel-assistant", "customer-support" ] } } ] } client.search(query, version="v2", filters=filters) ``` ```javascript JavaScript const query = "What do you know about me?"; const filters = { "AND":[ { "user_id":"alex" }, { "agent_id":{ "in":[ "travel-assistant", "customer-support" ] } } ] }; client.search(query, { version: "v2", filters }) .then(results => console.log(results)) .catch(error => console.error(error)); ``` ```bash cURL curl -X POST "https://api.mem0.ai/v1/memories/search/?version=v2" \ -H "Authorization : Token your - api - key" \ -H "Content-Type : application / json" \ -d '{ "query": "What do you know about me?", "filters": { "AND": [ { "user_id": "alex" }, { "agent_id": { "in": ["travel-assistant", "customer-support"] } } ] } }' ``` ```json Output [ { "id": "7f165f7e-b411-4afe-b7e5-35789b72c4a5", "memory": "Name: Alex. Vegetarian. Allergic to nuts.", "input": [ { "role": "user", "content": "Hi, I'm Alex. I'm a vegetarian and I'm allergic to nuts." }, { "role": "assistant", "content": "Hello Alex! I've noted that you're a vegetarian and have a nut allergy. I'll keep this in mind for any food-related recommendations or discussions." } ], "user_id": "alex", "hash": "9ee7e1455e84d1dab700ed8749aed75a", "metadata": null, "created_at": "2024-07-20T01:30:36.275141-07:00", "updated_at": "2024-07-20T01:30:36.275172-07:00" } ] ``` Example 2: Search using date filters ```python Python query = "What do you know about me?" filters = { "AND": [ {"created_at": {"gte": "2024-07-20", "lte": "2024-07-10"}}, {"user_id": "alex"} ] } client.search(query, version="v2", filters=filters) ``` ```javascript JavaScript const query = "What do you know about me?"; const filters = { "AND": [ {"created_at": {"gte": "2024-07-20", "lte": "2024-07-10"}}, {"user_id": "alex"} ] }; client.search(query, { version: "v2", filters }) .then(results => console.log(results)) .catch(error => console.error(error)); ``` ```bash cURL curl -X POST "https://api.mem0.ai/v1/memories/search/?version=v2" \ -H "Authorization: Token your-api-key" \ -H "Content-Type: application/json" \ -d '{ "query": "What do you know about me?", "filters": { "AND": [ { "created_at": { "gte": "2024-07-20", "lte": "2024-07-10" } }, { "user_id": "alex" } ] } }' ``` ```json Output [ { "id": "7f165f7e-b411-4afe-b7e5-35789b72c4a5", "memory": "Name: Alex. Vegetarian. Allergic to nuts.", "input": [ { "role": "user", "content": "Hi, I'm Alex. I'm a vegetarian and I'm allergic to nuts." }, { "role": "assistant", "content": "Hello Alex! I've noted that you're a vegetarian and have a nut allergy. I'll keep this in mind for any food-related recommendations or discussions." } ], "user_id": "alex", "hash": "9ee7e1455e84d1dab700ed8749aed75a", "metadata": null, "created_at": "2024-07-20T01:30:36.275141-07:00", "updated_at": "2024-07-20T01:30:36.275172-07:00" } ] ``` ### 4.3 Get All Users Get all users, agents, and sessions which have memories associated with them. ```python Python client.users() ``` ```javascript JavaScript client.users() .then(users => console.log(users)) .catch(error => console.error(error)); ``` ```bash cURL curl -X GET "https://api.mem0.ai/v1/entities/" \ -H "Authorization: Token your-api-key" ``` ```json Output [ { "id": "1", "name": "user123", "created_2024-07-17T16:47:23.899900-07:00", "updated_at": "2024-07-17T16:47:23.899918-07:00", "total_memories": 5, "owner": "alex", "organization": "alex-org", "metadata": {"foo": "bar"}, "type": "user" }, { "id": "2", "name": "travel-agent", "created_at": "2024-07-01T17:59:08.187250-07:00", "updated_at": "2024-07-01T17:59:08.187266-07:00", "total_memories": 10, "owner": "alex", "organization": "alex-org", "metadata": {"agent_id": "123"}, "type": "agent" } ] ``` ### 4.4 Get All Memories Fetch all memories for a user, agent, or session using the getAll() method. #### Get all memories of a user ```python Python user_memories = client.get_all(user_id="alex") ``` ```javascript JavaScript client.getAll({ user_id: "alex" }) .then(memories => console.log(memories)) .catch(error => console.error(error)); ``` ```bash cURL curl -X GET "https://api.mem0.ai/v1/memories/?user_id=alex" \ -H "Authorization: Token your-api-key" ``` ```json Output [ { "id":"f38b689d-6b24-45b7-bced-17fbb4d8bac7", "memory":"是素食主义者,对坚果过敏。", "agent_id":"travel-assistant", "hash":"62bc074f56d1f909f1b4c2b639f56f6a", "metadata":"None", "created_at":"2024-07-25T23:57:00.108347-07:00", "updated_at":"2024-07-25T23:57:00.108367-07:00" }, { "id":"0a14d8f0-e364-4f5c-b305-10da1f0d0878", "memory":"Will maintain personalized travel preferences for each user. Provide customized recommendations based on dietary restrictions, interests, and past interactions.", "agent_id":"travel-assistant", "hash":"35a305373d639b0bffc6c2a3e2eb4244", "metadata":"None", "created_at":"2024-07-26T00:31:03.543759-07:00", "updated_at":"2024-07-26T00:31:03.543778-07:00" } ] ``` #### Get all memories of an AI Agent ```python Python client.get_all(agent_id="ai-tutor") ``` ```javascript JavaScript client.getAll({ agent_id: "ai-tutor" }) .then(memories => console.log(memories)) .catch(error => console.error(error)); ``` ```bash cURL curl -X GET "https://api.mem0.ai/v1/memories/?agent_id=travel-assistant" \ -H "Authorization: Token your-api-key" ``` ```json Output [ { "id":"f38b689d-6b24-45b7-bced-17fbb4d8bac7", "memory":"是素食主义者,对坚果过敏。", "agent_id":"ai-tutor", "hash":"62bc074f56d1f909f1b4c2b639f56f6a", "metadata":"None", "created_at":"2024-07-25T23:57:00.108347-07:00", "updated_at":"2024-07-25T23:57:00.108367-07:00" }, { "id":"0a14d8f0-e364-4f5c-b305-10da1f0d0878", "memory":"My name is Alice.", "agent_id":"ai-tutor", "hash":"35a305373d639b0bffc6c2a3e2eb4244", "metadata":"None", "created_at":"2024-07-26T00:31:03.543759-07:00", "updated_at":"2024-07-26T00:31:03.543778-07:00" } ] ``` #### Get the short-term memories for a session ```python Python short_term_memories = client.get_all(user_id="alex123", session_id="trip-planning-2024") ``` ```javascript JavaScript client.getAll({ user_id: "alex123", session_id: "trip-planning-2024" }) .then(memories => console.log(memories)) .catch(error => console.error(error)); ``` ```bash cURL curl -X GET "https://api.mem0.ai/v1/memories/?user_id=alex123&session_id=trip-planning-2024" \ -H "Authorization: Token your-api-key" ``` ```json Output [ { "id":"06d8df63-7bd2-4fad-9acb-60871bcecee0", "memory":"Planning a trip to Japan next month. Interested in vegetarian restaurants in Tokyo.", "user_id":"alex123", "hash":"d2088c936e259f2f5d2d75543d31401c", "metadata":"None", "created_at":"2024-07-26T00:25:16.566471-07:00", "updated_at":"2024-07-26T00:25:16.566492-07:00" }, { "id":"b4229775-d860-4ccb-983f-0f628ca112f5", "memory":"Planning a trip to Japan next month. Interested in vegetarian restaurants in Tokyo.", "user_id":"alex123", "hash":"d2088c936e259f2f5d2d75543d31401c", "metadata":"None", "created_at":"2024-07-26T00:33:20.350542-07:00", "updated_at":"2024-07-26T00:33:20.350560-07:00" }, { "id":"df1aca24-76cf-4b92-9f58-d03857efcb64", "memory":"Planning a trip to Japan next month. Interested in vegetarian restaurants in Tokyo.", "user_id":"alex123", "hash":"d2088c936e259f2f5d2d75543d31401c", "metadata":"None", "created_at":"2024-07-26T00:51:09.642275-07:00", "updated_at":"2024-07-26T00:51:09.642295-07:00" } ] ``` #### Get specific memory ```python Python memory = client.get(memory_id="582bbe6d-506b-48c6-a4c6-5df3b1e63428") ``` ```javascript JavaScript client.get("582bbe6d-506b-48c6-a4c6-5df3b1e63428") .then(memory => console.log(memory)) .catch(error => console.error(error)); ``` ```bash cURL curl -X GET "https://api.mem0.ai/v1/memories/582bbe6d-506b-48c6-a4c6-5df3b1e63428" \ -H "Authorization: Token your-api-key" ``` ```json Output { "id":"06d8df63-7bd2-4fad-9acb-60871bcecee0", "memory":"Planning a trip to Japan next month. Interested in vegetarian restaurants in Tokyo.", "user_id":"alex123", "hash":"d2088c936e259f2f5d2d75543d31401c", "metadata":"None", "created_at":"2024-07-26T00:25:16.566471-07:00", "updated_at":"2024-07-26T00:25:16.566492-07:00" } ``` ### 4.5 Memory History Get history of how a memory has changed over time. ```python Python # Add some message to create history messages = [{"role": "user", "content": "I recently tried chicken and I loved it. I'm thinking of trying more non-vegetarian dishes.."}] client.add(messages, user_id="alex") # Add second message to update history messages.append({'role': 'user', 'content': 'I turned vegetarian now.'}) client.add(messages, user_id="alex") # Get history of how memory changed over time memory_id = "" history = client.history(memory_id) ``` ```javascript JavaScript // Add some message to create history let messages = [{ role: "user", content: "I recently tried chicken and I loved it. I'm thinking of trying more non-vegetarian dishes.." }]; client.add(messages, { user_id: "alex" }) .then(result => { // Add second message to update history messages.push({ role: 'user', content: 'I turned vegetarian now.' }); return client.add(messages, { user_id: "alex" }); }) .then(result => { // Get history of how memory changed over time const memoryId = result.id; // Assuming the API returns the memory ID return client.history(memoryId); }) .then(history => console.log(history)) .catch(error => console.error(error)); ``` ```bash cURL # First, add the initial memory curl -X POST "https://api.mem0.ai/v1/memories/" \ -H "Authorization: Token your-api-key" \ -H "Content-Type: application/json" \ -d '{ "messages": [{"role": "user", "content": "I recently tried chicken and I loved it. I'm thinking of trying more non-vegetarian dishes.."}], "user_id": "alex" }' # Then, update the memory curl -X POST "https://api.mem0.ai/v1/memories/" \ -H "Authorization: Token your-api-key" \ -H "Content-Type: application/json" \ -d '{ "messages": [ {"role": "user", "content": "I recently tried chicken and I loved it. I'm thinking of trying more non-vegetarian dishes.."}, {"role": "user", "content": "I turned vegetarian now."} ], "user_id": "alex" }' # Finally, get the history (replace with the actual memory ID) curl -X GET "https://api.mem0.ai/v1/memories//history/" \ -H "Authorization: Token your-api-key" ``` ```json Output [ { "id":"d6306e85-eaa6-400c-8c2f-ab994a8c4d09", "memory_id":"b163df0e-ebc8-4098-95df-3f70a733e198", "input":[ { "role":"user", "content":"I recently tried chicken and I loved it. I'm thinking of trying more non-vegetarian dishes.." }, { "role":"user", "content":"I turned vegetarian now." } ], "old_memory":"None", "new_memory":"Turned vegetarian.", "user_id":"alex123456", "event":"ADD", "metadata":"None", "created_at":"2024-07-26T01:02:41.737310-07:00", "updated_at":"2024-07-26T01:02:41.726073-07:00" } ] ``` ### 4.6 Update Memory Update a memory with new data. ```python Python message = "I recently tried chicken and I loved it. I'm thinking of trying more non-vegetarian dishes.." client.update(memory_id, message) ``` ```javascript JavaScript const message = "I recently tried chicken and I loved it. I'm thinking of trying more non-vegetarian dishes.."; client.update("memory-id-here", message) .then(result => console.log(result)) .catch(error => console.error(error)); ``` ```bash cURL curl -X PUT "https://api.mem0.ai/v1/memories/memory-id-here" \ -H "Authorization: Token your-api-key" \ -H "Content-Type: application/json" \ -d '{ "message": "I recently tried chicken and I loved it. I'm thinking of trying more non-vegetarian dishes.." }' ``` ```json Output { "id":"c190ab1a-a2f1-4f6f-914a-495e9a16b76e", "memory":"I recently tried chicken and I loved it. I'm thinking of trying more non-vegetarian dishes..", "agent_id":"travel-assistant", "hash":"af1161983e03667063d1abb60e6d5c06", "metadata":"None", "created_at":"2024-07-30T22:46:40.455758-07:00", "updated_at":"2024-07-30T22:48:35.257828-07:00" } ``` ### 4.7 Delete Memory Delete specific memory. ```python Python client.delete(memory_id) ``` ```javascript JavaScript client.delete("memory-id-here") .then(result => console.log(result)) .catch(error => console.error(error)); ``` ```bash cURL curl -X DELETE "https://api.mem0.ai/v1/memories/memory-id-here" \ -H "Authorization: Token your-api-key" ``` ```json Output {'message': 'Memory deleted successfully'} ``` Delete all memories of a user. ```python Python client.delete_all(user_id="alex") ``` ```javascript JavaScript client.deleteAll({ user_id: "alex" }) .then(result => console.log(result)) .catch(error => console.error(error)); ``` ```bash cURL curl -X DELETE "https://api.mem0.ai/v1/memories/?user_id=alex" \ -H "Authorization: Token your-api-key" ``` ```json Output {'message': 'Memories deleted successfully!'} ``` Delete all users. ```python Python client.delete_users() ``` ```javascript JavaScript client.delete_users() .then(users => console.log(users)) .catch(error => console.error(error)); ``` ```json Output {'message': 'All users, agents, and sessions deleted.'} ``` ### 4.8 Reset Client ```python Python client.reset() ``` ```javascript JavaScript client.reset() .then(result => console.log(result)) .catch(error => console.error(error)); ``` ```json Output {'message': 'Client reset successful. All users and memories deleted.'} ``` Fun fact: You can also delete the memory using the `add()` method by passing a natural language command: ```python Python client.add("Delete all of my food preferences", user_id="alex") ``` ```javascript JavaScript client.add("Delete all of my food preferences", { user_id: "alex" }) .then(result => console.log(result)) .catch(error => console.error(error)); ``` ```bash cURL curl -X POST "https://api.mem0.ai/v1/memories/" \ -H "Authorization: Token your-api-key" \ -H "Content-Type: application/json" \ -d '{ "messages": [{"role": "user", "content": "Delete all of my food preferences"}], "user_id": "alex" }' ``` ```json Output {'message': 'ok'} ``` If you have any questions, please feel free to reach out to us using one of the following methods: