Docs / PGN and FEN
FEN - accepted formats and validation
A FEN describes a position in six space-separated fields:
- piece placement (8 ranks separated by
/, from rank 8 to rank 1; digits 1-8 for empty squares;PNBRQKwhite,pnbrqkblack); - side to move (
worb); - castling rights (
KQkq, or-); - en passant square (or
-); - half-move clock (half-moves since the last capture or pawn move);
- 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
pnbrqkPNBRQKand digits 1-8 (a9is refused); - exactly one king per side;
- no pawn on the first or last rank;
- side to move is
worb; - 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
startFenof 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|blackfor 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.