ActionController::InvalidAuthenticityToken Ruby / Rails Origin: Application & API Last updated 15 Oct 2026

Rails: ActionController::InvalidAuthenticityToken (Can't verify CSRF token authenticity)

A non-GET request arrived without a valid CSRF token for the session.

Exact message you may see Can't verify CSRF token authenticity
Seen on: Ruby

Meaning

Forms built without Rails helpers, AJAX/fetch calls missing the X-CSRF-Token header, cached pages, cookies blocked, or API clients hitting a controller that expects CSRF protection.

Common causes

  • Form without authenticity_token (manual HTML)
  • fetch/AJAX without X-CSRF-Token header
  • Session cookie lost (domain, SameSite, cache)
  • API requests to a non-API controller

⚡ Quick fix

  1. Use form_with / include csrf_meta_tags and send the header
  2. Use ActionController::API or skip_forgery_protection for token-authenticated APIs
  3. Check cookies/session store

Detailed fix by platform

JavaScript

  1. javascript
    const token = document.querySelector('meta[name="csrf-token"]').content;
    fetch("/posts", { method: "POST", headers: { "X-CSRF-Token": token, "Content-Type": "application/json" }, body: JSON.stringify(data) });

How to diagnose

  1. Request — Token sent?
  2. Session — Cookie present?
  3. Controller — API or HTML?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

419 Page Expired — Laravel: 419 Page Expired (CSRF token mismatch) (PHP)419 Page ExpiredActiveRecord::PendingMigrationError — Rails: ActiveRecord::PendingMigrationError — Migrations are pending. To resolve this issue, run: bin/rails db:migrate (Ruby / Rails)ActiveRecord::Pending…Django CSRF — Forbidden (403) CSRF verification failed. Request aborted. (Python)Django CSRFinvalid csrf token — ForbiddenError: invalid csrf token (EBADCSRFTOKEN) (Authentication)invalid csrf tokenstate mismatch — OAuth callback error: state mismatch / Unable to verify authorization request state / State cookie was missing (Authentication)state mismatchundefined method for nil — Ruby: NoMethodError: undefined method `x' for nil (nil:NilClass) (Ruby / Rails)undefined method for …ActionCon…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.