I know of two APIs by which you can access Stockfish. The source code is available for both to implement yourself. In one case, the API is also hosted and can be queried freely.
(This answer is essentially the same as this answer that I gave to a similar, later question.)
I discovered this API through Fabian Fichter’s answer to this similar, later question.
The front-end is a PHP script and uses in addition MongoDB. It requires a Redis-compatible server for data storage. See the source code on GitHub.
The API is actively hosted and can be freely queried. For example, here’s a position from the Fried Liver Attack, with the FEN: “r1bqkb1r/ppp2ppp/5n2/n2Pp1N1/2B5/8/PPPP1PPP/RNBQK2R w KQkq - 1 6”
r1bqkb1r/ppp2ppp/5n2/n2Pp1N1/2B5/8/PPPP1PPP/RNBQK2R w KQkq - 1 6
For example, to find just the best move, form the query like:
In this case:
which gives you this result:
which translates to Bb5+, agreeing with theory.
See the GitHub link for a little additional documentation, such as using
querypv to get continuations.
Ilya Zhelyabuzhsky’s Python wrapper for Stockfish
See Ilya Zhelyabuzhsky’s Python wrapper (a) on GitHub and (b) available for installation from PyPI.
This requires that Stockfish be installed on the server.
This Python package implements a class to integrate Stockfish with Python.
For example, you can define a position from a FEN and then request the best move:
stockfish.set_fen_position("rnbqkbnr/pppp1ppp/4p3/8/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 2")
See either of the above links for additional documentation.