GraphQL API Integration

Installing the Sourcegraph GraphQL API

Installation Time: ~5 minutes
Latest Version: 5.11
1

Generate an Access Token

To authenticate with the GraphQL API, you'll need an access token. Generate one on your Sourcegraph instance:

Generate Token URL
https://sourcegraph.example.com/user/settings/tokens
2

Test the API

Run a simple query to verify your installation. Use the following command to echo your username:

Bash
curl -H 'Authorization: token YOUR_TOKEN' \
-d '{"query": "query { currentUser { username } }"}' \
https://sourcegraph.example.com/.api/graphql

You should see a response like this:

JSON Response
{ "data": { "currentUser": { "username": "YOUR_USERNAME" } } }
3

Explore the API Console

Sourcegraph includes a built-in API console for writing queries and viewing documentation. Access it at:

API Console URL
https://sourcegraph.example.com/api/console

You can also test the API on the Sourcegraph.com API console.