Skip to main content
Knowledge Graph, our graph-based retrieval-augmented generation (RAG), achieves higher accuracy than traditional RAG approaches that use vector retrieval. This guide will help you understand and use the Knowledge Graph API to integrate RAG capabilities into your agents.
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.
Knowledge Graphs can be deployed org-wide (accessible to every team in the organization) or deployed to specific teams. Both are accessible via the API and SDK.
  • With an org-scoped API key, control team scope from the request body: pass team_ids on create or update, or filter list with the team_ids query parameter. Omit team_ids on list to see only org-wide Knowledge Graphs.
  • With a team-scoped API key, every request is automatically restricted to the key’s team. Create assigns to that team; list and retrieve return only that team’s Knowledge Graphs; body team_ids is not accepted.

Create a Knowledge Graph

A Knowledge Graph is a collection of files that are used to answer questions. To start working with a Knowledge Graph, create an empty Knowledge Graph that you can add files to. Endpoint: POST https://api.writer.com/v1/graphs

Request body

The request body is a JSON object that contains the following fields:

Response format

The response has the following structure:

Deploy a Knowledge Graph to specific teams

Pass one or more team IDs in the team_ids array to deploy a new Knowledge Graph to those teams instead of the whole organization. Members of any listed team can use the Knowledge Graph; members of other teams cannot.
To find team IDs, open a team in AI Studio and copy the numeric ID from the URL. To later change the team assignment of an existing Knowledge Graph, see Update team assignment.
Find your Knowledge Graph ID using one of these methods:

Add a file to a Knowledge Graph

Once you’ve created a Knowledge Graph, you can add files to it. The files you add to a Knowledge Graph are used to answer questions and enhance the accuracy of the responses from the LLM You must upload the file to the Writer API before adding it to a Knowledge Graph. See Manage files for more information about how to upload files. Endpoint: POST /v1/graphs/{graph_id}/file
The response will have this structure:

Path parameters

Request body

The request body is a JSON object that contains the following fields: You must upload the file to the Writer API before adding it to a Knowledge Graph. See Manage files for more information about how to upload files.

Remove a file from a Knowledge Graph

Endpoint: DELETE /v1/graphs/{graph_id}/file/{file_id}

Path parameters

Response format

The response has this structure:

List Knowledge Graphs by team

Retrieve Knowledge Graphs in an organization with the list endpoint. By default, the response contains only org-wide Knowledge Graphs. To also include Knowledge Graphs deployed to specific teams, pass those team IDs in the team_ids query parameter. Endpoint: GET /v1/graphs
Omitting team_ids returns only org-wide Knowledge Graphs. Knowledge Graphs deployed to specific teams do not appear in an unfiltered list; the caller must opt in by passing team_ids. Team-scoped API keys are the exception: those requests always return only that key’s team, regardless of team_ids.

Update team assignment

Change the teams a Knowledge Graph is deployed to by including team_ids in a PUT /v1/graphs/{graph_id} request. The array you pass replaces the whole team assignment:
  • Omit team_ids to leave the current team assignment unchanged.
  • Pass one or more team IDs to scope the Knowledge Graph to exactly those teams.
  • Pass an empty array ([]) to remove every team assignment and make the Knowledge Graph org-wide again.
Team-scoped API keys cannot change the team assignment of a Knowledge Graph; the field is only accepted from org-scoped API keys.

Use a team-scoped API key

Team-scoped API keys restrict every Knowledge Graph API request to a single team, without any per-request configuration. They are useful when a team owns its own API integration and should not see other teams’ Knowledge Graphs. When a team-scoped API key is used:
  • POST /v1/graphs automatically assigns the new Knowledge Graph to the key’s team. Passing team_ids in the request body is rejected.
  • PUT /v1/graphs/{graph_id} cannot change the team assignment of a Knowledge Graph. Passing team_ids in the request body is rejected.
  • GET /v1/graphs/{graph_id}, DELETE /v1/graphs/{graph_id}, and the file endpoints return only Knowledge Graphs deployed to the key’s team. Org-wide Knowledge Graphs and Knowledge Graphs deployed to other teams are not visible.
  • GET /v1/graphs returns only Knowledge Graphs deployed to the key’s team. Passing team_ids in the query string is only accepted when it matches the key’s team.
Create team-scoped API keys the same way you create org-scoped API keys, then select the team the key applies to. Existing org-scoped API keys continue to work exactly as before.

Add URLs to a Knowledge Graph

You can add URLs to a Knowledge Graph to enhance the model’s knowledge with content from websites. The LLM can then query and use this information when responding to questions. This example adds a web connector URL to a Knowledge Graph. It first retrieves the Knowledge Graph to see the current URLs and then adds a new URL to the Knowledge Graph. Note that this operation replaces the entire list of URLs attached to the Knowledge Graph, which is why it is important to retrieve the existing list of URLs first.
See more examples in Add URLs to a Knowledge Graph.

Add a Knowledge Graph to a no-code agent

After you’ve created a Knowledge Graph, you can add it to a no-code agent to add RAG capabilities. You can use this endpoint to add or remove Knowledge Graphs from a no-code agent. Endpoint: PUT /v1/applications/{application_id}/graphs

Path parameters

Request body

The request body is a JSON object that contains the following fields: This method updates the associated Knowledge Graphs to the exact list of IDs provided in the request. To remove a single Knowledge Graph from the no-code agent, set the graph_ids parameter to an array containing the IDs of all the other Knowledge Graphs associated with the no-code agent, excluding the ID of the Knowledge Graph to remove. To remove all Knowledge Graphs from the no-code agent, set this parameter to an empty array.

Response format

The response contains the new list of IDs of the Knowledge Graphs associated with the no-code agent.

Next steps