## Knowledge Graph Question Endpoint

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](https://dev.writer.com/home/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`

### Example Requests

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

#### Python
```python
from writerai import Writer

client = Writer()

response = client.graphs.question(
  graph_ids=["<GRAPH_ID>"],
  question="<QUESTION>"
)

print(f"Answer: {response.answer}")
print(f"Sources: {response.sources}")
```

#### JavaScript
```javascript
import { Writer } from "writer-sdk";

const client = new Writer();

const response = await client.graphs.question({
  graph_ids: ["<GRAPH_ID>"],
  question: "<QUESTION>"
});

console.log(`Answer: ${response.answer}`);
console.log(`Sources: ${response.sources}`);
```

### Request Body

The request body is a JSON object with the following fields:

| Parameter      | Type          | Required | Description                                                                                                                                               |
|----------------|---------------|----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------|
| `graph_ids`    | array[string] | Yes      | The unique identifiers of the Knowledge Graphs to query. You can specify multiple Knowledge Graph IDs to search across multiple graphs.                   |
| `question`     | string        | Yes      | The question to answer using the Knowledge Graph.                                                                                                        |
| `subqueries`   | Boolean       | No       | Specify whether to include subqueries. Defaults to `false`.                                                                                              |
| `stream`       | Boolean       | No       | Determines whether to stream the response. If `true`, the output is sent as it is generated. Defaults to `false`.                                        |
| `query_config` | object        | No       | Configuration options for Knowledge Graph queries.                                                                                                       |

### Response Format

The response is a JSON object with the following fields:

| Parameter   | Type    | Description                                                                                             |
|-------------|---------|---------------------------------------------------------------------------------------------------------|
| `question`  | string  | The question you asked.                                                                                 |
| `answer`    | string  | The answer to the question based on the Knowledge Graph content.                                        |
| `sources`   | array   | An array of source objects that contain the file information and text snippets used to generate the answer. |
| `subqueries`| array   | An array of subquery objects. Only included if `subqueries` is `true`.                                   |

### Examples
#### Query Multiple Knowledge Graphs
You can query multiple Knowledge Graphs at once by providing multiple graph IDs in the `graph_ids` array.

### 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.

### 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.

## Next Steps
- Learn how to [create and manage Knowledge Graphs](https://dev.writer.com/home/knowledge-graph)
- Use Knowledge Graphs in chat completions with [tool calling](https://dev.writer.com/home/kg-chat)
- Explore [Knowledge Graph API reference](https://dev.writer.com/api-reference/kg-api)
