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
query field (the GraphQL query string) and an optional variables field.
Authentication
- Access token
- OAuth token
- Sudo token
Generate a personal access token under Settings > Access tokens, then pass it in the
Authorization header: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/consoledirectly. - 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 underrateLimits.
| Setting | Default | Description |
|---|---|---|
graphQLMaxDepth | 30 | Maximum nesting depth of objects in a query |
graphQLMaxFieldCount | 500,000 | Maximum number of fields in a response |
graphQLMaxAliases | 500 | Maximum number of aliases in a single query |
graphqlMaxDuplicateFieldCount | 500 | Maximum duplicate fields in a query |
graphqlMaxUniqueFieldCount | 500 | Maximum unique fields in a query |
graphQLMaxFieldCount limit when working with large data sets.