# 0301-functions-graphql-get-nonce

<!--
This file is auto-generated by scripts/functions/generate-function-docs.js.
Do not edit manually.
-->
---
title: graphql_get_nonce
docType: function
plugin: wp-graphql
---

# graphql_get_nonce

```php
graphql_get_nonce(): string;
```

Generate a WPGraphQL nonce for cookie-based authentication. This nonce is required for authenticated GraphQL HTTP requests that use WordPress cookie authentication. It provides CSRF protection by proving the request originated from a legitimate WordPress-generated page. Example usage in JavaScript: ```javascript fetch('/graphql', \{ headers: \{ 'Content-Type': 'application/json', 'X-WP-Nonce': wpGraphQLSettings.nonce \}, body: JSON.stringify(\{ query: '\{ viewer \{ name \} \}' \}) \}); ``` Example usage in PHP for localizing to JavaScript: ```php wp_localize_script( 'my-script', 'wpGraphQLSettings', [ 'nonce'    => graphql_get_nonce(), 'endpoint' => graphql_get_endpoint(), ] ); ```

- **Since:** 2.6.0
- **Source File:** `plugins/wp-graphql/access-functions.php`

## Return

- `string`: The WPGraphQL nonce string.

## Source

- [`plugins/wp-graphql/access-functions.php:750`](https://github.com/wp-graphql/wp-graphql/blob/main/plugins/wp-graphql/access-functions.php#L750)

```php
function graphql_get_nonce(): string {
```