Vari in Your Code

Helpful Resource

Creating Variables Video

To use vari in your codebase, first do this:

$ npx poly generate

The generate command will generate a fresh version of the Poly library, with all the latest functions and variables.

If you just did the Vari in Your Code tutorial, you should now see the vari.test.test1 variable in your Poly library.

Vari Get

Now let’s get the value of a variable in code and log it to the console!

Save the following code inside your Poly test project as variTest.ts:

import { vari } from 'polyapi';

(async () => {
    const myvar = await vari.test.test1.get();
    console.log(myvar);
})();

Then run the code via:

$ npx ts-node variTest.ts

That’s it! You should now see the value of the variable “abc” in your console.

Now you can go forth and use Vari in all your Poly functions to coordinate configuration and secrets!

Vari Other Methods

Each vari variable has the following methods:

  • get: get the value of a variable

  • inject: inject the value of a secret variable (more below)

  • update: update the value of a variable

  • onUpdate: listen for updates of a variable and run some code when it happens

update and onUpdate should be pretty self-explanatory.

But let’s dive into inject a bit more and talk about when you use it, versus when you use get!

Secret vs Non-Secret Variables

In PolyAPI, variables can be either secret or non-secret.

Secret variables are encrypted and stored in a secure vault , while non-secret variables are stored in plain text.

Furthermore, non-secret variables can be accessed on local developers machines through the use of the myvar.get() method.

Secret variables, on the other hand, may only be used in server functions running on PolyAPI’s servers.

To use a secret variable, you must use the myvar.inject() method instead of .get().

That will instruct your server function to pull the value of the secret variable from the secure vault when it is executing.

If you try to use .get() on a secret variable, you will get an error.

Conclusion

That’s it! You now know how to use vari in your codebase.

If you have any questions or need help, please don’t hesitate to reach out to us at support@polyapi.io