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": "<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.
query_configobjectNoConfiguration options for Knowledge Graph queries. See Query configuration parameters below for detailed parameter descriptions.

Query configuration parameters

ParameterTypeRangeDefaultDescription
max_subquestionsinteger1-106Maximum number of sub-questions to generate when processing complex queries.
search_weightinteger0-10050Controls the balance between keyword and semantic search in ranking results.
grounding_levelnumber0.0-1.00.0Controls how closely responses must match to source material. Set lower for grounded outputs, higher for creativity.
max_snippetsinteger5-25 (recommended)30Maximum number of text snippets to retrieve from the Knowledge Graph for context. Works in concert with search_weight to control best matches vs broader coverage. Note: While technically supports 1-60, values below 5 may return no results due to RAG implementation. Recommended range is 5-25. Due to RAG system behavior, you may see more snippets than requested.
max_tokensinteger100-80004000Maximum number of tokens the model can generate in the response.
keyword_thresholdnumber0.0-1.00.7Threshold for keyword-based matching when searching Knowledge Graph content.
semantic_thresholdnumber0.0-1.00.7Threshold for semantic similarity matching when searching Knowledge Graph content. Set higher for stricter relevance, lower for broader range.
inline_citationsBooleantrue/falsefalseWhether to include inline citations within the response text.
For detailed explanations and usage examples, see the Knowledge Graph query configuration guide.

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": "<QUESTION>",
    "stream": true
  }'

Next steps