Skip to main content
The Sourcegraph GraphQL API provides broad access to data stored and computed by your Sourcegraph instance. It is primarily intended for diagnostics and simple tooling.
The GraphQL API is an internal interface without formal backwards-compatibility guarantees. It may change across Sourcegraph releases. For external integrations, use the REST API introduced in Sourcegraph 7.0. If your use case is not yet covered by the REST API, contact support@sourcegraph.com.
For code search integrations, use the streaming search API instead. It is purpose-built for consuming search results and is the same API used by the Sourcegraph UI.

Endpoint

Send requests as JSON with a query field (the GraphQL query string) and an optional variables field.

Authentication

Generate a personal access token under Settings > Access tokens, then pass it in the Authorization header:
A successful authentication check returns:

Example queries

Verify authentication

Search for code

Look up a repository

Read a file

API console

Sourcegraph ships with a built-in API console where you can write queries and browse the schema interactively.
  • Navigate to Settings > Debug console in your Sourcegraph instance, or visit https://sourcegraph.example.com/debug/console directly.
  • Click Docs on the right side of the console to browse the schema documentation.
  • You can also use the Sourcegraph.com debug console to test against the latest API version.

Sourcegraph CLI

The src-cli provides a command-line interface to the GraphQL API. It reads your access token and instance URL from a config file or environment variables, handles JSON escaping, and lets you pipe multi-line queries easily.

Query cost limits

To protect system stability, Sourcegraph enforces limits on GraphQL query complexity. These defaults can be adjusted in site configuration under rateLimits. Use pagination where available to stay within the graphQLMaxFieldCount limit when working with large data sets.