| 1 | --- |
| 2 | title: "hx-config" |
| 3 | description: "Configures request behavior with JSON" |
| 4 | --- |
| 5 | |
| 6 | The `hx-config` attribute allows you to configure request behavior using JSON. |
| 7 | |
| 8 | ## Syntax |
| 9 | |
| 10 | ```html |
| 11 | <button hx-post="/api/users" hx-config='{"timeout": 5000}'> |
| 12 | Create User |
| 13 | </button> |
| 14 | ``` |
| 15 | |
| 16 | ## Available Options |
| 17 | |
| 18 | | Option | Type | Description | Example | |
| 19 | |---------------|---------|----------------------------------------------------------|---------------| |
| 20 | | `timeout` | number | Request timeout in milliseconds | 5000 | |
| 21 | | `credentials` | string | Fetch credentials mode: "omit", "same-origin", "include" | "include" | |
| 22 | | `cache` | string | Fetch cache mode: "default", "no-cache", "reload", etc. | "no-cache" | |
| 23 | | `redirect` | string | Fetch redirect mode: "follow", "error", "manual" | "follow" | |
| 24 | | `referrer` | string | Referrer URL or "no-referrer" | "no-referrer" | |
| 25 | | `integrity` | string | Subresource integrity value | "sha384-..." | |
| 26 | | `validate` | boolean | Whether to validate form before submission | true | |
| 27 | |
| 28 | These options map to the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API). |
| 29 | |
| 30 | > **Security note:** The `mode` option is intentionally excluded from `hx-config`. It is always |
| 31 | > reset to the global `htmx.config.mode` value (default: `"same-origin"`) to prevent injection |
| 32 | > attacks from widening request scope. To change the fetch mode, set it globally via |
| 33 | > `htmx.config.mode` or a `<meta name="htmx-config">` tag. See the |
| 34 | > [mode config reference](/reference/config/htmx-config-mode) and |
| 35 | > [security best practices](/docs#best-practices) for details. |
| 36 | |
| 37 | ## Merging Configuration |
| 38 | |
| 39 | By default, child `hx-config` values replace parent configurations entirely. Use the `+` prefix to merge with parent config instead: |
| 40 | |
| 41 | ```html |
| 42 | <div hx-config='{"timeout": 5000}'> |
| 43 | <button hx-get="/data" |
| 44 | hx-config='{"+headers": {"X-Custom": "value"}}'> |
| 45 | Load Data |
| 46 | </button> |
| 47 | </div> |
| 48 | ``` |
| 49 | |
| 50 | The button inherits the timeout and adds a custom header. |
| 51 | |
| 52 | Without `+`, the child config replaces the parent config entirely. |
| 53 | |
| 54 | ## Examples |
| 55 | |
| 56 | ### Set timeout for slow endpoint |
| 57 | |
| 58 | ```html |
| 59 | <button hx-get="/slow-report" |
| 60 | hx-config='{"timeout": 30000}'> |
| 61 | Generate Report |
| 62 | </button> |
| 63 | ``` |
| 64 | |
| 65 | ### Include credentials |
| 66 | |
| 67 | ```html |
| 68 | <button hx-get="/api/data" |
| 69 | hx-config='{"credentials": "include"}'> |
| 70 | Load Data |
| 71 | </button> |
| 72 | ``` |
| 73 | |
| 74 | ### Disable cache |
| 75 | |
| 76 | ```html |
| 77 | <button hx-get="/live-data" |
| 78 | hx-config='{"cache": "no-cache"}'> |
| 79 | Get Live Data |
| 80 | </button> |
| 81 | ``` |