Cr4shBarcode
Cr4shBarcode3mo ago

How to capture current date and use the returned result in an API call

In it's current iteration, my bot asks the user to input the current date for use in an API call. I don't want the user to have to do that. After some research I learned that the code: string date = System.DateTime.Today.ToString("ddmmyy"); can be used to get the current date. But how can I run it and pass the result through as a variable for my Agent API get request?
No description
3 Replies
Tico | Voiceflow Assistant
You don’t need to ask the user – just create the date in-flow, save it to a Voiceflow variable, and reference that variable in your API Step. 1. Add a Code step (or a Custom Function) just before your API Step.
Paste something like this (JavaScript runs natively in Code/Function steps):
/* assumes a Voiceflow variable called current_date already exists */
const today = new Date();

// format the date however your API expects
// examples ----------------------------------------------------
variables.current_date_iso = today.toISOString(); // 2025-06-21T02:21:27.000Z
variables.current_date_ymd = today.toISOString().slice(0,10); // 2025-06-21
variables.current_date_dmy = today.toLocaleDateString('en-GB'); // 21/06/2025
// -------------------------------------------------------------
/* assumes a Voiceflow variable called current_date already exists */
const today = new Date();

// format the date however your API expects
// examples ----------------------------------------------------
variables.current_date_iso = today.toISOString(); // 2025-06-21T02:21:27.000Z
variables.current_date_ymd = today.toISOString().slice(0,10); // 2025-06-21
variables.current_date_dmy = today.toLocaleDateString('en-GB'); // 21/06/2025
// -------------------------------------------------------------
2. In your API Step use the variable(s) exactly the same way you use any other Voiceflow variable, e.g.:
GET https://api.example.com/report?date={current_date_ymd}
GET https://api.example.com/report?date={current_date_ymd}
or in a JSON body:
{
"date": "{current_date_iso}"
}
{
"date": "{current_date_iso}"
}
That’s it—the user never sees a prompt, and the date is automatically injected into every request. Docs for reference: https://docs.voiceflow.com
W. Williams (SFT)
tico is wrong Create a VF var called current_date and then do:
const today = new Date()
current_date = today.toISOString().slice(0,10)
const today = new Date()
current_date = today.toISOString().slice(0,10)
That will give you the current date / time in UTC time. If you want it in local date / time then do (for US Central):
const today = new Date()
today.toLocaleString('en-US', { timeZone: 'America/Chicago' })
const today = new Date()
today.toLocaleString('en-US', { timeZone: 'America/Chicago' })
@Cr4shBarcode
Cr4shBarcode
Cr4shBarcodeOP3mo ago
Thank you so much! 😊

Did you find this page helpful?