| [ Web Proxy ] |
| Viewing: https://docs.sourcegraph.com/api/graphql | [Back] [Original] |
The Sourcegraph GraphQL API is a debug API that can be used for Sourcegraph diagnostics and simple tooling.
The GraphQL API is intended primarily for debugging use cases. It does not have backwards-compatibility guarantees, and may not remain stable across Sourcegraph releases. For more information, please refer to the Sourcegraph API page.
For code search integrations, we currently recommend using the stream search API.
There are two ways to authenticate with the Sourcegraph GraphQL API:
Generate an access token for your Sourcegraph instance under Settings > Access tokens.
Then run this query to echo your username back:
BASHcurl -H 'Authorization: token YOUR_TOKEN' -d '{"query": "query { currentUser { username } }"}' https://sourcegraph.example.com/.api/graphql
If you have an OAuth app configured with the user:all scope, you can use OAuth Bearer tokens:
BASHcurl -H 'Authorization: Bearer YOUR_OAUTH_TOKEN' -d '{"query": "query { currentUser { username } }"}' https://sourcegraph.example.com/.api/graphql
Both authentication methods will return a response like this:
JSON{"data": {"currentUser": {"username": "YOUR_USERNAME"}}}
OAuth access tokens are always short-lived and must be refreshed using
refresh tokens before expiration. Check the expires_in field and refresh
proactively.
For automated scripts, CI/CD pipelines, and production integrations, use service accounts instead of personal access tokens. Service accounts are designed specifically for programmatic API access and provide better security and audit capabilities.
Sourcegraph includes a built-in API console that lets you write queries and view debug API documentation in your browser.
You can find the debug API console at any time by going to Settings, and then
clicking Debug console from the left sidebar, or by visiting it directly at
https://sourcegraph.example.com/debug/console.
If you have not yet set up a Sourcegraph server, you can also test out the API on the Sourcegraph.com debug console (which always uses the latest version of the API).
To access the documentation, click Docs on the right-hand side of the debug console page.
Site admins may create access tokens with the special site-admin:sudo scope, which allows the holder to perform any action as any other user.
BASHcurl -H 'Authorization: token-sudo user="SUDO_TO_USERNAME",token="YOUR_TOKEN"' -d '{"query": "query { currentUser { username } }"}' https://sourcegraph.example.com/.api/graphql
This scope is useful when building Sourcegraph integrations with external services where the service needs to communicate with Sourcegraph and does not want to force each user to individually authenticate to Sourcegraph.
A command line interface to Sourcegraph's API is available. Today, it is roughly the same as using the API via curl (see below), but it offers a few nice things:
curl properly.curl command using the src api -get-curl flag.To learn more, see sourcegraph/src-cli
The entire API can be used via curl (or any HTTP library), just the same as any other GraphQL API. For example:
BASHcurl -H 'Authorization: token YOUR_TOKEN' -d '{"query":"query($query: String!) { search(query: $query) { results { matchCount } } }","variables":{"query":"Router"}}' https://sourcegraph.com/.api/graphql
i.e. you just need to send the Authorization header and a JSON object like {"query": "my query string", "variables": {"var1": "val1"}}.
To ensure system performance and stability, configurable GraphQL query cost limitations have been implemented. This feature is crucial for preventing resource exhaustion due to extensive or overly complex queries. The default configuration looks as follows, and can be modified in site configuration:
SHELL"rateLimits": { "graphQLMaxAliases": 500, "graphQLMaxDepth": 30, "graphQLMaxFieldCount": 500000 },
| Web Proxy Viewer | New URL | Original Page |