Skip to content

Update Document

Updates the document with the specified ID.

Provided parameters will be updated, and all other parameters will be left unchanged.

Endpoint

POST /v20241104/documents/{id}
Path parameter Type Details
{id} string Unique ID of the document to be updated.
Example Endpoint
POST /v20241104/documents/PbOrIZIBzlg4zQqVFxxE    

API request

  • Optional Parameters


    content string

    The main content of the document, in one of the supported formats.

    Info
    • Supported formats: plaintext | html | markdown

    • Formats other than plain text will be converted to plain text before being stored in the semantic index.

    • To use Gainly for searching files (such as PDFs, Microsoft Word docs, etc.) in your app, you must first convert the file content to one of the supported formats before including it in this field.

    Set it to null by passing it a null value.

    MaxLength: 100 KB


    content_type string (enum)

    Content type of the content field. Default is plaintext.

    Valid values: plaintext | html | markdown


    created_at string

    ISO 8601 formatted string that indicates the creation time of the document.

    Example: 2023-10-05T14:48:02Z


    language string (enum)

    Language code (of a supported language) that indicates the language of the document.

    Default is en.


    metadata object

    JSON object containing metadata related to the document.

    Metadata update logic for a document
    • New key-value pairs will be merged with the existing ones.
    • Passing new values for existing keys will overwrite the old values.
    • To unset individual keys, pass a null value for them.
    • To clear all metadata, pass null for the entire metadata field.

    Max number of keys: 100

    MaxLength for a key: 50 characters

    MaxLength for a value: 500 characters


    source_uri string

    Source URI of the document. Typically a unique address/location/identifier (within your app) for the document.

    Info

    When a user clicks on a search result that corresponds to this document, you can use its source_uri to navigate the user to the correct location within your app.

    Set it to null by passing it a null value.

    MaxLength: 1000


    tenant_id string

    Tenant ID of the document owner.

    MinLength: 1   MaxLength: 250


    title string

    Title of the document, in plain text.

    MinLength: 1   MaxLength: 250


    updated_at string

    ISO 8601 formatted string that indicates the last modification time of the document.

    If not provided in the API request, it defaults to the timestamp when the document was updated via the API.

    Example: 2023-10-05T14:48:02Z

  • POST /v20241104/documents/{id}


    curl -X POST "https://api.gainly.ai/v20241104/documents/YOUR_DOCUMENT_ID_HERE" \  # (1)!
      -H "Content-Type: application/json" \
      -H "X-API-Key: YOUR_API_KEY_HERE" \  # (2)!
      -d '{
        "title": "My awesome document title edited"
      }'
    
    1. Replace YOUR_DOCUMENT_ID_HERE with the document ID you want to update.
    2. Replace YOUR_API_KEY_HERE with the value of your API key.
    # Prompt for AI coding assistants/IDEs (e.g., ChatGPT, Claude, GitHub Copilot, Cursor, Windsurf)
    
    Using the Gainly API:
    1. Write code to call the update_document operation (see OpenAPI spec: https://api.gainly.ai/v20241104/openapi.json)
    2. Implement authentication using the header "X-API-Key" as described in the docs: https://docs.gainly.ai/latest/api-reference/authentication/
    3. Implement rate limit handling as described in the docs: https://docs.gainly.ai/latest/api-reference/rate-limits/
    4. Implement error handling
    5. Handle the response according to the DocumentUpdateResponse schema in the OpenAPI spec
    
    using System.Net.Http;
    using System.Text.Json;
    
    var client = new HttpClient();
    
    var id = "YOUR_DOCUMENT_ID_HERE"; // (1)!
    var url = $"https://api.gainly.ai/v20241104/documents/{id}";
    
    var payload = new {
        title = "My awesome document title edited"
    };
    
    client.DefaultRequestHeaders.Add("X-API-Key", "YOUR_API_KEY_HERE"); // (2)!
    
    var response = await client.PostAsJsonAsync(url, payload);
    var result = await response.Content.ReadAsStringAsync();
    Console.WriteLine(result);
    
    1. Replace YOUR_DOCUMENT_ID_HERE with the document ID you want to update.
    2. Replace YOUR_API_KEY_HERE with the value of your API key.
    package main
    
    import (
        "fmt"
        "strings"
        "net/http"
    )
    
    func main() {
        id := "YOUR_DOCUMENT_ID_HERE" // (1)!
        url := fmt.Sprintf("https://api.gainly.ai/v20241104/documents/%s", id)
    
        payload := `{
            "title": "My awesome document title edited"
        }`
    
        req, _ := http.NewRequest("POST", url, strings.NewReader(payload))
        req.Header.Set("Content-Type", "application/json")
        req.Header.Set("X-API-Key", "YOUR_API_KEY_HERE") // (2)!
    
        resp, _ := http.DefaultClient.Do(req)
        defer resp.Body.Close()
    
        var result map[string]interface{}
        json.NewDecoder(resp.Body).Decode(&result)
        fmt.Println(result)
    }
    
    1. Replace YOUR_DOCUMENT_ID_HERE with the document ID you want to update.
    2. Replace YOUR_API_KEY_HERE with the value of your API key.
    import java.net.http.HttpClient;
    import java.net.http.HttpRequest;
    import java.net.http.HttpResponse;
    import java.net.URI;
    
    var client = HttpClient.newHttpClient();
    
    var id = "YOUR_DOCUMENT_ID_HERE"; // (1)!
    var url = "https://api.gainly.ai/v20241104/documents/" + id;
    
    var payload = """
        {
            "title": "My awesome document title edited"
        }
        """;
    
    var request = HttpRequest.newBuilder()
        .uri(URI.create(url))
        .header("Content-Type", "application/json")
        .header("X-API-Key", "YOUR_API_KEY_HERE") // (2)!
        .POST(HttpRequest.BodyPublishers.ofString(payload))
        .build();
    
    var response = client.send(request, HttpResponse.BodyHandlers.ofString());
    System.out.println(response.body());
    
    1. Replace YOUR_DOCUMENT_ID_HERE with the document ID you want to update.
    2. Replace YOUR_API_KEY_HERE with the value of your API key.
    const axios = require('axios');  // or: import axios from 'axios';
    
    const id = 'YOUR_DOCUMENT_ID_HERE'; // (1)!
    const url = `https://api.gainly.ai/v20241104/documents/${id}`;
    
    const payload = {
        title: 'My awesome document title edited'
    };
    
    const headers = {
        'Content-Type': 'application/json',
        'X-API-Key': 'YOUR_API_KEY_HERE' // (2)!
    };
    
    axios.post(url, payload, { headers })
        .then(response => console.log(response.data))
        .catch(error => console.error('Error:', error.message));
    
    1. Replace YOUR_DOCUMENT_ID_HERE with the document ID you want to update.
    2. Replace YOUR_API_KEY_HERE with the value of your API key.
    <?php
    
    $client = new \GuzzleHttp\Client();
    
    $id = 'YOUR_DOCUMENT_ID_HERE'; # (1)!
    $url = "https://api.gainly.ai/v20241104/documents/{$id}";
    
    $response = $client->request('POST', $url, [
        'json' => [
            'title' => 'My awesome document title edited'
        ],
        'headers' => [
            'Content-Type' => 'application/json',
            'X-API-Key' => 'YOUR_API_KEY_HERE' # (2)!
        ],
    ]);
    
    echo $response->getBody();        
    
    1. Replace YOUR_DOCUMENT_ID_HERE with the document ID you want to update.
    2. Replace YOUR_API_KEY_HERE with the value of your API key.
    import requests
    
    id = "YOUR_DOCUMENT_ID_HERE" # (1)!
    url = f"https://api.gainly.ai/v20241104/documents/{id}"
    
    payload = {
        "title": "My awesome document title edited"
    }
    
    headers = {
        "Content-Type": "application/json",
        "X-API-Key": "YOUR_API_KEY_HERE" # (2)!
    }
    
    response = requests.post(url, json=payload, headers=headers)
    
    data = response.json()
    print(data)
    
    1. Replace YOUR_DOCUMENT_ID_HERE with the document ID you want to update.
    2. Replace YOUR_API_KEY_HERE with the value of your API key.
    require 'json'
    require 'uri'
    require 'net/http'
    require 'openssl'
    
    id = 'YOUR_DOCUMENT_ID_HERE' # (1)!
    url = URI("https://api.gainly.ai/v20241104/documents/#{id}")
    
    http = Net::HTTP.new(url.host, url.port)
    http.use_ssl = true
    
    request = Net::HTTP::Post.new(url)
    request['Content-Type'] = 'application/json'
    request['X-API-Key'] = 'YOUR_API_KEY_HERE' # (2)!
    request.body = {
        title: 'My awesome document title edited'
    }.to_json
    
    response = http.request(request)
    puts response.read_body
    
    1. Replace YOUR_DOCUMENT_ID_HERE with the document ID you want to update.
    2. Replace YOUR_API_KEY_HERE with the value of your API key.

API Response

{
    "id": "PbOrIZIBzlg4zQqVFxxE",
    "object": "document",
    "title": "My awesome document title edited",
    "content": "This document has great content. Can be really long, but is short here.",
    "metadata": null,
    "tenant_id": "tenant123",
    "language": "en",
    "source_uri": "/doc/7-my-awesome-document",
    "created_at": "2023-10-05T14:48:02Z",
    "updated_at": "2023-11-02T05:23:10Z",
    "token_usage": {
        "semantic_tokens": 25,     // determined by the size of 'title', 'content' fields
        "llm_tokens": {
            "llm_output_tokens": 0,
            "llm_input_tokens": 0,
            "model": null
        }
    },
    "livemode": false
}

Returns the document object is the update succeeded.