Search and retrieve documents

This guide provides examples of how to use the main functions of the Developer Knowledge API: searching for documents, retrieving a single document, and retrieving multiple documents in a batch.

Before you begin, make sure you have enabled the API and generated a Developer Knowledge API key, and save your key to an environment variable:

export DEVELOPERKNOWLEDGE_API_KEY="YOUR_API_KEY"

Search for documents with SearchDocumentChunks

Use the documents.searchDocumentChunks method to find document chunks that match a query string. The results include chunks of content from the document and a parent reference that you can use to retrieve the full document content.

The following example searches for documents matching "BigQuery":

curl "https://developerknowledge.googleapis.com/v1alpha/documents:searchDocumentChunks?query=BigQuery&key=$DEVELOPERKNOWLEDGE_API_KEY"

You can refine search results using parameters like page_size to limit the number of results per page and page_token to retrieve subsequent pages.

Retrieve a document with GetDocument

Use the documents.get method to retrieve the full content of a single document, using the DocumentChunk.parent returned by a SearchDocumentChunks call.

The following example retrieves a document with the name documents/DOCUMENT_ID:

curl "https://developerknowledge.googleapis.com/v1alpha/documents:get?name=documents/DOCUMENT_ID&key=$DEVELOPERKNOWLEDGE_API_KEY"

The response is the full Document resource, including the markdown_content field.

Retrieve multiple documents with BatchGetDocuments

Use the documents.batchGet method to retrieve up to 100 documents by name in a single API call. This is more efficient than making multiple GetDocument requests.

The following example retrieves two documents by name:

curl "https://developerknowledge.googleapis.com/v1alpha/documents:batchGet?names=documents/DOCUMENT_ID_1&names=documents/DOCUMENT_ID_2&key=$DEVELOPERKNOWLEDGE_API_KEY"

The response contains a list of the requested Document resources.