Skip to content

Commit bcb2038

Browse files
GraphQL API Authentication (#1863)
1 parent e2b9ccf commit bcb2038

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

content/collections/pages/graphql.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,6 +1525,39 @@ EntriesQuery::auth(function () {
15251525
});
15261526
```
15271527

1528+
## Authentication
1529+
1530+
Out of the box, the GraphQL API is publicly accessible.
1531+
1532+
You can restrict access to the API by adding the `STATAMIC_GRAPHQL_AUTH_TOKEN` key to your `.env` file. It should be set to a long, random string.
1533+
1534+
```php
1535+
STATAMIC_GRAPHQL_AUTH_TOKEN=a-long-random-string
1536+
```
1537+
1538+
Then, when you make requests to the GraphQL API, you'll need to include the token in the `Authorization` header, like this:
1539+
1540+
```curl
1541+
curl -X GET "https://example.com/graphql" \
1542+
-H "Authorization: Bearer a-long-random-string" \
1543+
-H "Accept: application/json"
1544+
-d '{"query": "{ping}"}'
1545+
```
1546+
1547+
### Authenticating users
1548+
1549+
If you want to authenticate based on users, we recommend using [Laravel Sanctum](https://laravel.com/docs/master/sanctum) instead.
1550+
1551+
To use Sanctum, you'll need to [store users in the database](/tips/storing-users-in-a-database) and add the `auth:sanctum` middleware in the `graphql.php` config.
1552+
1553+
```php
1554+
// config/statamic/graphql.php
1555+
1556+
'middleware' => [
1557+
'auth:sanctum',
1558+
],
1559+
```
1560+
15281561
## Custom fields
15291562

15301563
You can add fields to certain types by using the `addField` method on the facade.

0 commit comments

Comments
 (0)