To charge the PW, set the backup reserve to 100% (or your desired state of charge).
To discharge, you first need to set a time-of-use rate plan in the Tesla app (i.e. you need a peak period during which it can discharge). Then using the API, enable Time-Based Control mode, Export Everything, and a lower backup reserve if needed, and the PW should start discharging.
Use the GET API endpoint to check the state of charge and decide when to stop charging/discharging.
Is there a way to tell my powerwall to charge/discharge via the API?
Ideally it would be nice to also set the percentage ie if my powerwall is 5kwh asking for 50% discharge will start discharging at 2.5kwh.