> For the complete documentation index, see [llms.txt](https://cde-inc.gitbook.io/cde-cad-documentation/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cde-inc.gitbook.io/cde-cad-documentation/fivem-developer-integration/troubleshooting.md). Agents can also search and read this documentation through its [MCP server](https://cde-inc.gitbook.io/cde-cad-documentation/~gitbook/mcp).

# Troubleshooting

## 400 Bad Request

{% hint style="warning" %}
**Cloudflare Body Stripping**

Some hosting configurations cause Cloudflare to strip the JSON body from requests made by FiveM's `PerformHttpRequest`. If you receive a `400` error indicating missing fields, use the `x-payload` header workaround:

1. Base64-encode your JSON body
2. Send it as the `x-payload` header value
3. Send an empty JSON object `{}` as the actual body

```lua
-- Base64 fallback example
local payload = json.encode({ callType = '911', location = 'Main St' })
local encoded = _base64Encode(payload)  -- your Base64 function

PerformHttpRequest(url, callback, 'POST', '{}', {
    ['Content-Type'] = 'application/json',
    ['x-api-key']    = Config.ApiKey,
    ['x-payload']    = encoded
})
```

This workaround is supported on every `/api/fivem/*` and `/api/civilian/fivem-*` endpoint.
{% endhint %}

## 401 Unauthorized

{% hint style="danger" %}
**Common Causes**

* **Missing API key.** Ensure the `x-api-key` header is included in every authenticated request.
* **Invalid API key.** Verify the key starts with `fvm_` and is exactly 68 characters long (`fvm_` + 64 hex chars).
* **Regenerated key.** If you regenerated your API key, update your `config.lua` with the new key.
* **Wrong header name.** The header must be `x-api-key` (lowercase, with hyphens). Not `Authorization`, not `apiKey`.
  {% endhint %}

## 403 Forbidden

* **Community deactivated.** The community was disabled by the platform. Contact CDE support.
* **Cross-community attempt.** You passed a `communityId` in body/query that does not match the community owning the API key.
* **Feature disabled.** Some endpoints (ERS, bridges) require the matching feature toggle to be enabled under **FiveM Settings**.

## 429 Too Many Requests

{% hint style="warning" %}
**Rate Limits**

Every endpoint is rate-limited. High-impact actions (911 submissions, panic alerts, ERS callouts, LB Phone text) have stricter per-community limits than read-only lookups.

When rate-limited, wait for the cooldown period before retrying. The response includes a `Retry-After` header indicating seconds to wait. Apply client-side cooldowns to player-triggered actions (911 commands, panic key-binds) so a single player cannot exhaust the community's allowance.
{% endhint %}

## SafeRequest with Retry

Use this utility to automatically retry failed requests with exponential backoff and the Cloudflare workaround:

```lua
-- server/utils.lua
function SafeRequest(url, method, body, headers, callback, attempt)
    attempt = attempt or 1
    local maxAttempts = 3

    PerformHttpRequest(url, function(statusCode, responseText, responseHeaders)
        -- Success
        if statusCode >= 200 and statusCode < 300 then
            if callback then callback(statusCode, responseText) end
            return
        end

        -- Rate limited - wait and retry
        if statusCode == 429 and attempt < maxAttempts then
            local retryAfter = tonumber(responseHeaders['Retry-After']) or 5
            local delay = retryAfter * 1000 * attempt
            print('[CDE-CAD] Rate limited. Retrying in ' .. (delay / 1000) .. 's (attempt ' .. attempt .. ')')
            SetTimeout(delay, function()
                SafeRequest(url, method, body, headers, callback, attempt + 1)
            end)
            return
        end

        -- Body stripped by Cloudflare - retry with x-payload
        if statusCode == 400 and attempt == 1 and body and body ~= '{}' then
            print('[CDE-CAD] Retrying with x-payload header...')
            local encoded = _base64Encode(body)
            headers['x-payload'] = encoded
            SafeRequest(url, method, '{}', headers, callback, attempt + 1)
            return
        end

        -- Final failure
        print('[CDE-CAD] Request failed: ' .. method .. ' ' .. url .. ' -> ' .. tostring(statusCode))
        if callback then callback(statusCode, responseText) end
    end, method, body or '{}', headers)
end
```
