Is there a way to provide environment variables to a VSTS CI npm task?

You can add some build variables to the build definition and then reference those in your build steps somewhere. For example, for your API_URL add a build variable with the same name and value. If you need the variable to be secret for any reason (passwords, etc.) just click the lock icon next to the value field.

enter image description here

Then add a new cmd task to your build and move it to the top to set your environment variables before you start your build. The way you reference the build variables is like this...

set API_URL=$(Build.API_URL)

In the UI it will look like this:

enter image description here

I added two cmd tasks to a test build just to show that it is working. The first one is used to set the environment variable and then I used the second to dump all the environment variables so I could see them. You can see in the build logs that it worked.

enter image description here

If you want to reference the build variables using something other than the command line you can find examples of the different ways to reference build variables here. There are also examples of how to use secret variables on that page.

EDIT:

Since you have some concerns about it not being available in your node app I tested it out in the console real quick to show that it will definitely work.

enter image description here