PHPackages                             l2iterative/bonsai-sdk-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. l2iterative/bonsai-sdk-php

ActiveLibrary[API Development](/categories/api)

l2iterative/bonsai-sdk-php
==========================

Bonsai SDK in PHP

1.2.0(2y ago)32Apache-2.0PHP

Since Jan 6Pushed 2y ago1 watchersCompare

[ Source](https://github.com/l2iterative/bonsai-sdk-php)[ Packagist](https://packagist.org/packages/l2iterative/bonsai-sdk-php)[ RSS](/packages/l2iterative-bonsai-sdk-php/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)Dependencies (7)Versions (4)Used By (0)

PHP SDK for Bonsai API
----------------------

[](#php-sdk-for-bonsai-api)

[![a guy sitting on the subway station waiting for the train to arrive](title.png)](title.png)

The current Bonsai API implementation is written in Rust. Since Bonsai requires an API key and would consume the quotas to generate the zero-knowledge proof, web3 applications built with Bonsai would need a backend.

Like most web3 applications, the backend can be ***minimalistic***, because most of the on-chain interactions can be completed through, for example, [Metamask's Ethereum provider API](https://docs.metamask.io/wallet/reference/provider-api/), in the frontend.

### How would the backend look like?

[](#how-would-the-backend-look-like)

For the aforementioned reasons, backends often fall into the following six classes:

- **Pure:**
    - A pure Typescript implementation, probably with the use of WebAssembly, often implemented through Rust (see [wasm-bindgen](https://github.com/rustwasm/wasm-bindgen))
    - A pure PHP implementation
    - A pure Python implementation, such as [Django](https://www.djangoproject.com/) and [Flask](https://github.com/pallets/flask)
- **With [foreign function interface (FFI)](https://en.wikipedia.org/wiki/Foreign_function_interface):**
    - A Typescript implementation with FFI to high-performance code likely written in Rust, through [ffi-rs](https://github.com/zhangyuang/node-ffi-rs) or [koffi](https://github.com/Koromix/koffi)
    - A PHP implementation using FFI, probably through [the FFI extension](https://www.php.net/manual/en/book.ffi.php)
    - A Python implementation using FFI, probably through [CFFI](https://cffi.readthedocs.io/en/latest/)

Numerous factors will affect which one to go:

- **Availability of web hosting providers.**
    - There are many affordable web hosting platforms for Typescript and PHP.
    - To run Python, the tradition is to use a full-fledged cloud virtual machine (such as an AWS EC2 instance). There is a preference to avoid using a full-fledged VM due to cost and ability to auto-scale.
- **Performance.**
    - Everyone would agree that Python is slow.
    - PHP can be optimized through the use of [OPcache](https://www.php.net/manual/en/book.opcache.php)to precompile the script, but it is not machine code. Typescript can be compiled (together with WebAssembly) into machine code through [v8](https://v8.dev/), which we can expect to have the best performance among the "pure" group.
    - Nevertheless, it becomes less relevant when we can use FFI because one can also run machine code, and unlike code generated by [just-in-time compilation (JIT)](https://en.wikipedia.org/wiki/Just-in-time_compilation), such code is [ahead-of-time compilation (AOT)](https://en.wikipedia.org/wiki/Ahead-of-time_compilation), and is excepted to be more performant and should be the go-to whenever available.
- **Developer friendliness.**
    - Probably most important: ideally with few learning cycles, no hacking around.
    - Rust is useful here, as RISC Zero programs are now written in Rust, and WebAssembly from Rust has been used in web development, particularly for cryptography.
    - The other programming language is better similar to Rust. Among all the three, PHP is most similar. PHP is known for very friendly to beginners.
    - All there have available Web3 SDK implementation, but Typescript has best support nowadays.

It is important to know that this backend is going to be minimalistic. We have the backend because the API key cannot be exposed to the client. The backend may just consist of:

- access to a database, such as [MySQL](https://www.mysql.com/), for off-chain data storage
- integration with a spam prevention system, such as [Cloudflare Turnstile](https://www.cloudflare.com/products/turnstile/) or [Google reCAPTCHA](https://www.google.com/recaptcha/about/)
- connecting to RISC Zero Bonsai API, which is the purpose of this repository

### How to use

[](#how-to-use)

The documentation can be found in the [docs](docs) folder. It is designed to resemble the Bonsai Rust API here:

The integration tests can be served as tutorials. It can be as simple as follows:

```
