Docs / PGN and FEN

FEN - accepted formats and validation

A FEN describes a position in six space-separated fields:

  1. piece placement (8 ranks separated by /, from rank 8 to rank 1; digits 1-8 for empty squares; PNBRQK white, pnbrqk black);
  2. side to move (w or b);
  3. castling rights (KQkq, or -);
  4. en passant square (or -);
  5. half-move clock (half-moves since the last capture or pawn move);
  6. full-move number.

Server-side validation

Checks performed:

  • not empty, at least 4 fields (3 fields is refused; the two counters are optional);
  • 8 ranks, each totalling 8 squares, with only pnbrqkPNBRQK and digits 1-8 (a 9 is refused);
  • exactly one king per side;
  • no pawn on the first or last rank;
  • side to move is w or b;
  • the side that does not have the move is not in check (this also rules out adjacent kings);
  • the half-move clock must be an integer; extra spaces between fields are tolerated.

X-FEN (KQkq) and Shredder-FEN (HFhf, file letters) are accepted for Chess960.

Not checked (accepted as is), so a FEN can be “valid” and still absurd:

  • castling rights inconsistent with the position (castling announced without a rook, or with the king off its home square);
  • an absurd en passant square, or one with no pawn able to capture;
  • excess material (for example sixteen pawns);
  • a negative move number.

For analysis caching, a position is normalized on its first four fields: two FENs that differ only by the counters are the same position. An en passant square only counts when an enemy pawn can really capture; otherwise it is equivalent to -.

Where a FEN is used

  • startFen of a course ::diagram (diagram tag): validated with the same rules when the page is saved, even without moves.
  • The [FEN] tag of a PGN (PGN syntax); [SetUp] is not needed.
  • Position editor: the address is /editor/<FEN> where the 8 ranks are separated by / and the other fields by _ instead of spaces, for example /editor/rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR_b_KQkq_e3_0_1. The editor is lenient: missing fields get defaults (white to move, no castling, no en passant, counters 0 and 1). In Chess960 mode it writes castling rights as file letters.
  • Opening explorer: the ?fen= address parameter.
  • Position image: GET /fen/<URL-safe base64 FEN>/preview.png, with ?side=white|black for the orientation (default: side to move). Use base64 without =, + replaced by - and / by _. An invalid FEN returns an error 400.

Chess960

A Chess960 starting FEN has pawns on ranks 2 and 7, mirrored pieces on ranks 1 and 8, the king between the two rooks and bishops on opposite-colored squares. In a PGN, add [Variant "Chess960"] next to [FEN].

Common pitfalls

  • A rank whose squares do not add up to 8: refused.
  • Wrong side to move while the opponent’s king is in check: refused.
  • Two kings of the same color, or a pawn on rank 1 or 8: refused.
  • Rank 8 comes first in a FEN.
  • Always validate a FEN you did not compute yourself, and check castling rights and en passant by eye: the validation does not.