How to create a Custom code FlexBalancer

Creating your very first simple Custom Balancer

First of all, log in and proceed to the FlexBalancers page:


Select a name for your Balancer, choose `Custom code` button and press `Next Step`:


Then, fill in your `fallback` address. You may also change default `TTL`, or keep it equal to 10. After that, press `Create New FlexBalancer` button:


Now, you have text editor open, so you can write or post there your custom code.

Supported language: `TypeScript`. 
Editor: Monaco Editor, see Monaco Editor Accessibility Guide for detailed info. Notice, that you also are able to edit FlexBalancer description, Fallback and TTL.

Let's create our first Custom FlexBalancer with the simplest logic: we will check uptime of `jsDelivr CDN` and if it is bigger that 98,5 - return answer '' with TTL 25, if not - return '' with default TTL (that you have set before).

* If jsDelivr uptime is bigger than 98,5 - return '' with TTL 25
* else return '' with default TTL
function onRequest(req: IRequest, res: IResponse) {
if(fetchCdnRumUptime('jsdelivr-cdn') > 98.5) { // if jsDelivr uptime is high
res.setAddr(''); // we set answer address to ''
res.setTTL(25); // with TTL = 25

return; // return answer
// if jsDelivr uptime is lower than expected
res.setAddr(''); // we set answer address to ''
return; // return answer

The text editor checks your code syntax for errors, shows error details on mouse hover and won't allow you to publish code with errors:


It also 'knows' all definitions of PerfOps Custom Answers API (functions, type values for particular user) and provides helpful hints (use `Ctrl+Space`) and auto-completion (use `Tab`):



Now all changes are done and no errors remain, you simply press `Test and Publish` button and confirm the deployment:


The next page has important information, do not forget to copy/save `CNAME` record for further placement into domain settings for your domain, then press `Done`:


Now you can see your newly created FlexBalancer at `FlexBalancers` page:


Let's check if your Custom Answer works. After some time, when you already have made all nessessary `CNAME` changes - check CDN RUM Uptime at CDNPerf.

You can see that RUM Uptime is higher than `98,5`.


So, according to rules that you have defined at Custom Answer, it should return `` as response address.


And it does! That means your FlexBalancer works perfectly!

You may want to take a look at our Tutorial and at more complicated script samples stored at our repository.