The Knowledge Graph question endpoint allows you to ask questions directly to one or more Knowledge Graphs. This provides a direct way to query your Knowledge Graphs without needing to use chat completions. You can also enable subqueries to break down complex questions into smaller subqueries and provide answers for each one.
You need an API key to access the Writer API. Get an API key by following the steps in the API quickstart.We recommend setting the API key as an environment variable in a .env file with the name WRITER_API_KEY.

Question endpoint

Endpoint: POST /v1/graphs/question
curl --location --request POST https://api.writer.com/v1/graphs/question \
  --header "Authorization: Bearer $WRITER_API_KEY" \
  --header "Content-Type: application/json" \
  --data-raw '{
    "graph_ids": ["GRAPH_ID"],
    "question": "YOUR QUESTION"
  }'

Request body

To find the ID of your Knowledge Graph, you can:
The request body is a JSON object with the following fields:
ParameterTypeRequiredDescription
graph_idsarray[string]YesThe unique identifiers of the Knowledge Graphs to query. You can specify multiple Knowledge Graph IDs to search across multiple graphs.
questionstringYesThe question to answer using the Knowledge Graph.
subqueriesBooleanNoSpecify whether to include subqueries. Defaults to false.
streamBooleanNoDetermines whether to stream the response. If true, the output is sent as it is generated, which can be useful for real-time applications. Defaults to false. See how to handle streaming responses below.

Response format

The response is a JSON object with the following fields:
ParameterTypeDescription
questionstringThe question you asked.
answerstringThe answer to the question based on the Knowledge Graph content.
sourcesarrayAn array of source objects that contain the file information and text snippets used to generate the answer.
sources[].file_idstringIf the source is a file, this is the unique identifier of the file that contains the source information.
sources[].snippetstringIf the source is a file, this is a snippet of text from the source file used to answer the question.
subqueriesarrayAn array of subquery objects. Only included if subqueries is true.
subqueries[].querystringThe subquery the model generated to answer the question.
subqueries[].answerstringThe answer to the subquery.
subqueries[].sourcesarrayAn array of source objects for the subquery.
{
  "question": "What was our company's revenue growth in Q4 2023?",
  "answer": "The company achieved 23% year-over-year revenue growth in Q4 2023, reaching $142 million in total revenue.",
  "sources": [
    {
      "file_id": "1234",
      "snippet": "In the fourth quarter of 2023, we saw strong performance with revenue reaching $142 million, representing a 23% increase compared to Q4 2022."
    }
  ]
}

Examples

Query multiple Knowledge Graphs

You can query multiple Knowledge Graphs at once by providing multiple graph IDs in the graph_ids array.
curl --location --request POST https://api.writer.com/v1/graphs/question \
  --header "Authorization: Bearer $WRITER_API_KEY" \
  --header "Content-Type: application/json" \
  --data-raw '{
    "graph_ids": [
      "GRAPH_ID_1",
      "GRAPH_ID_2"
    ],
    "question": "What are the key findings from our research documents?"
  }'

Enable subqueries

When you enable subqueries, the model breaks down complex questions into smaller subqueries and provides answers for each one from the Knowledge Graphs.
curl --location --request POST https://api.writer.com/v1/graphs/question \
  --header "Authorization: Bearer $WRITER_API_KEY" \
  --header "Content-Type: application/json" \
  --data-raw '{
    "graph_ids": ["GRAPH_ID"],
    "question": "What are the main products and their market performance?",
    "subqueries": true
  }'

Stream the response

You can stream the response from the Knowledge Graphs by setting the stream parameter to true. This is useful for real-time applications that need to see the response as it is generated.
curl --location --request POST https://api.writer.com/v1/graphs/question \
  --header "Authorization: Bearer $WRITER_API_KEY" \
  --header "Content-Type: application/json" \
  --data-raw '{
    "graph_ids": ["GRAPH_ID"],
    "question": "YOUR QUESTION",
    "stream": true
  }'

Next steps