Document: Upload v1

The upload document API endpoint allows you to upload documents to a memory in Langbase with API. This endpoint requires a User or Org API key.

This endpoint can also be used to replace an existing document in a memory. To do this, you need to provide the same documentName and memoryName attributes as the existing document. We also have a separate guide on how to replace an existing document in memory.


Generate a User/Org API key

You will need to generate an API key to authenticate your requests. For more information, visit the User/Org API key documentation.


POST/v1/memory/documents

Step #1Get SignedUrl to Upload Document

Uploading a document to a memory requires a signed URL. POST a request to this endpoint to get a signed URL and use PUT method to upload the document.

Headers

  • Name
    Content-Type
    Type
    string
    Required
    Required
    Description

    Request content type. Needs to be application/json.

  • Name
    Authorization
    Type
    string
    Required
    Required
    Description

    Replace <YOUR_API_KEY> with your user/org API key.

Body Parameters

  • Name
    memoryName
    Type
    string
    Required
    Required
    Description

    Name of the memory to which the document will be uploaded.

  • Name
    documentName
    Type
    string
    Required
    Required
    Description

    Name of the document.

Optional attributes

  • Name
    meta
    Type
    object
    Description

    Custom metadata for the document, limited to string key-value pairs. A maximum of 10 pairs is allowed.

Deprecated attributes

  • Name
    fileName
    Type
    string
    Description

    Name of the document.

POST
/v1/memory/documents
import {Langbase} from 'langbase';
import {readFileSync} from 'fs';

const langbase = new Langbase({
  apiKey: process.env.LANGBASE_API_KEY!,
});

async function main() {
  const hasDocumentUploaded = await langbase.memory.documents.upload({
    memoryName: 'knowledge-base',
    contentType: 'application/pdf',
    documentName: 'technical-doc.pdf',
    document: readFileSync('document.pdf'),
    meta: {
      category: 'technical',
      section: 'overview',
    },
  });

  if (hasDocumentUploaded.ok) {
    console.log('Document uploaded successfully');
  }
}

main();

Response

  • Name
    Response
    Type
    object
    Description

    The response object returned by the API endpoint.

    Response

    interface Response {
      signedUrl: string;
    }
    
    • Name
      signedUrl
      Type
      string
      Description

      Signed URL that can be used to upload a document.

API Response

{
  "signedUrl": "https://b.langbase.com/..."
}

PUT{SignedUrl}

Step #2Upload Document on SignedUrl

Use the signed URL to upload the document to the memory. The signed URL is valid for 2 hours.

Headers

  • Name
    Content-Type
    Type
    string
    Required
    Required
    Description

    Request content type. Needs to be the MIME type of the document. Currently, we support application/pdf, text/plain, text/markdown, text/csv, and all major code files as text/plain. For csv, pdf, text, and markdown files, it should correspond to the file type used in the documentName attribute in step 1. For code files, it should be text/plain.

Body Parameters

  • Name
    body
    Type
    FileBody
    Required
    Required
    Description

    The body of the file to be stored in the bucket. It can be Buffer, File, FormData, or ReadableStream type.

Usage example

Install the SDK

npm i langbase

Environment variables

.env file

LANGBASE_API_KEY="<USER/ORG-API-KEY>"

Upload Document

PUT
{SignedUrl}
import requests

def upload_document(signed_url, file):
  with open(file_path, 'rb') as file:
    headers = {'Content-Type': 'application/pdf'}
    response = requests.put(signed_url, headers=headers, data=file)
  return response

Response

  • Name
    Response
    Type
    object
    Description

    The response object returned by the API endpoint.

    Response

    interface Response {
      ok: boolean;
      status: number;
      statusText: string;
    }
    
    • Name
      ok
      Type
      boolean
      Description

      Indicates whether the upload was successful.

    • Name
      status
      Type
      number
      Description

      HTTP status code of the upload response.

    • Name
      statusText
      Type
      string
      Description

      HTTP status message corresponding to the status code.

API Response

{
  "ok": true,
  "status": 200,
  "statusText": "OK"
}