An event is a name with listeners : your functions that run when the event is emitted. Each listener is written in its own language. Your code emits the event, a client emits it through a system API, or API Maker emits it after a successful call of an API you attach it to. So the side effects of an operation (mails, stock, exports) stay out of the API itself.
In short
An event is a name with listeners : your functions that run when the event happens.
It happens when your code emits it, or by itself after an API you attach it to : an order saved.
Use it for what comes after : an email, a stock update, an export. The API that saved the order stays simple.
The events, in folders.
Its listeners : the code that runs when the event happens.
Turn on Automatically trigger on API hit, then add the APIs under Trigger on APIs. After each successful call of one of them, API Maker emits the event in the background : the caller does not wait for the listeners.
The event data (g.req.eventData) is the whole answer of that call : { success, statusCode, data, … }.
A call that failed emits nothing. A call failed when its status is 400 or more, or when its answer says "success": false.
A call refused before the API ran (no token, no access) emits nothing either.
A call from your code (g.sys.db…) emits it too, like a call over HTTP.
import*asTfrom'types';import*asdbfrom'db-interfaces';asyncfunctionmain(g:T.IAMGlobal){constdata=g.req.eventData;// what was emittedawaitg.sys.db.saveSingleOrMultiple({instance:'mongodb',database:'shop',collection:'notifications',saveData:{order_no:data.order_no,text:`Order ${data.order_no} placed`},});return{notified:true};// returned to the emitter}module.exports=main;
fromamimportGasyncdefmain(g:G):data=g.req.event_data# what was emittedawaitg.sys.db.save_single_or_multiple('mongodb','shop','notifications',save_data={'order_no':data.order_no,'text':f'Order {data.order_no} placed'},)return{'notified':True}# returned to the emitter
<?phpfunctionmain(AmG$g){$data=$g->req->eventData;// what was emitted$g->sys->db->saveSingleOrMultiple('mongodb','shop','notifications',saveData:['order_no'=>$data->order_no,'text'=>"Order {$data->order_no} placed"],);return['notified'=>true];// returned to the emitter}
filestaticclassApi{publicstaticasyncTask<object?>Main(Gg){varorderNo=g.Req.EventData?["order_no"]?.GetValue<long>();// what was emittedawaitg.Sys.Db.SaveSingleOrMultiple(newSave("mongodb","shop","notifications",new{order_no=orderNo,text=$"Order {orderNo} placed"}));returnnew{notified=true};// returned to the emitter}}
packagecodeimport("fmt""apimaker/am")funcMain(g*am.G)(any,error){data:=am.AsM(g.Req.EventData())// what was emitted_,err:=g.Sys.Db.SaveSingleOrMultiple(am.Save{Instance:"mongodb",Database:"shop",Collection:"notifications",SaveData:am.M{"order_no":data.Get("order_no"),"text":fmt.Sprintf("Order %v placed",data.Get("order_no"))},})iferr!=nil{returnnil,err}returnam.M{"notified":true},nil// returned to the emitter}
useam::{Errors,G,Save,Value,json};pubfnmain(g:&mutG)->Result<Value,Errors>{letorder_no=g.req.event_data()["order_no"].clone();// what was emittedg.sys.db.save_single_or_multiple(Save::new("mongodb","shop","notifications",json!({"order_no":order_no,"text":format!("Order {order_no} placed")}),))?;Ok(json!({"notified":true}))// returned to the emitter}
importam.G;importam.JsonObject;importam.Save;importjava.util.Map;classApi{staticObjectmain(Gg){JsonObjectdata=JsonObject.from(g.req.getEventData());// what was emittedg.sys.db.saveSingleOrMultiple(newSave("mongodb","shop","notifications",Map.of("order_no",data.getLong("order_no"),"text","Order "+data.getLong("order_no")+" placed")));returnMap.of("notified",true);// returned to the emitter}}
importam.Gimportam.JsonObjectimportam.Savefunmain(g:G):Any?{valdata=JsonObject.from(g.req.eventData)// what was emittedg.sys.db.saveSingleOrMultiple(Save("mongodb","shop","notifications",mapOf("order_no"todata.getLong("order_no"),"text"to"Order ${data.getLong("order_no")} placed")),)returnmapOf("notified"totrue)// returned to the emitter}
Every listener gets the same g as a custom API : g.req.eventData holds the emitted data, g.sys reaches every API, g.logger writes to the log table.
Listeners run one after the other, each within its timeout : in the sandbox and with runOnNativeProcess alike. Empty : sandboxReqTimeout of the configuration (13 seconds), whatever time the request which emitted the event has.
A listener may emit another event, which may emit another. API Maker keeps the list of the events already running for the request : an emit which comes back to one of them is stopped, so a chain never becomes an infinite loop. Each level of depth is allowed ; only the cycle is refused.
Events and their listeners are files in Git and deploy with a pull. Listeners have versions.
Test a listener from the API testing page : pick the event, the listener, put the event data in the body and send.
On the Events page, Move up and Move down order the listeners, Grid View lists every listener of every event, Clone copies an event with its listeners and Copy full event name gives the name to emit.
On the native process, console.log is not captured : use g.logger.