DEV Community

Cover image for I built an API that detects chess tactical patterns from FEN and PGN
stevejvv
stevejvv

Posted on

I built an API that detects chess tactical patterns from FEN and PGN

I've been working on ChessGrammar, an API that takes a chess
position (FEN) or a full game (PGN) and returns every tactical
pattern it finds.

What it detects

10 patterns: fork, pin, skewer, discovered attack, double check,
back rank mate, smothered mate, deflection, interference,
trapped piece.

For each tactic, it returns the trigger move, target pieces,
material gain, and the forced sequence.

How it works

The engine uses a two-phase approach:

  • Depth 1 — fast geometric detection. Scans piece relationships to find pattern candidates in ~5ms per position.
  • Depth 2 — sequence confirmation. Verifies that the tactic works against best defense by computing forcing sequences.

No Stockfish dependency at runtime. The engine runs custom
heuristic algorithms built on top of python-chess.

Try it

There's a playground where you can paste any FEN or PGN
and see the results — no signup, no API key:

chessgrammar.com/playground

Example

Send this FEN (a knight fork with discovered attack):

6k1/5p1p/4p3/4q3/3n4/2Q3P1/PP1N1P1P/6K1 b - - 3 37
Enter fullscreen mode Exit fullscreen mode

The API returns two tactics: a fork (knight attacks king
and queen) and a discovered attack on the queen, both
with a +7.0 material gain.

API

curl -X POST https://chessgrammar.com/api/v1/extract \
  -H "Content-Type: application/json" \
  -d '{"fen": "6k1/5p1p/4p3/4q3/3n4/2Q3P1/PP1N1P1P/6K1 b - - 3 37"}'
Enter fullscreen mode Exit fullscreen mode

Full docs at chessgrammar.com/docs.

What's next

More patterns (hanging piece, overload), forcing tree
confirmation, batch analysis, and a game evaluation
endpoint. Full roadmap in the docs.

Feedback

If you try the playground and something looks wrong,
there's a bug report button built in. I'd love to hear
what you think — especially if you've worked on chess
analysis tools before.

Top comments (0)