RudderStack injects the metadata function as the second argument to transformEvent and transformBatch. Use it to access event metadata and customize your transformation logic based on source, destination, or connection mode.
What you can do
Read source and destination identifiers for routing or filtering
Access the unique message ID for each event
Check the connection mode (deviceMode or cloudMode) in hybrid mode destinations
Access the device mode token (Custom-Authorization) for tokenization validation
What it returns
An object containing the following properties when available:
Property
Description
sourceId
The source ID from the Settings tab of your source.
sourceName
The source name set in the RudderStack dashboard. For example, JavaScript-QA, TestJSSource.
destinationId
The destination ID from the Settings tab of your destination.
messageId
The unique ID for each event.
sourceType
The source type, for example, Android, iOS.
destinationType
The destination type where RudderStack sends the transformed event, for example, Snowflake.
mode
The connection mode: deviceMode or cloudMode. Available for hybrid mode destinations.
Custom-Authorization
The device mode token when tokenization is enabled.
RudderStack injects an asynchronous fetch function in JavaScript transformations. Use it to make external API calls and enrich events with data from third-party services.
What you can do
Make HTTP requests (GET, POST, PUT, DELETE, and others) to external APIs
Enrich events with data from services like Clearbit, IP geolocation APIs, or custom backends
Pass custom headers and request bodies
What it returns
The response from the API call in JSON format.
For JavaScript transformations, RudderStack imposes a 4 second execution timeout limit. This limit does not include the time for fetch or fetchV2 calls to complete. RudderStack recommends using batch API requests instead of a separate request per event when possible.
fetchV2 is a wrapper for the fetch call in JavaScript transformations. Use it when you need structured access to response properties such as status code, headers, and body, or when you need timeout control.
What you can do
Make external API calls with structured response handling
Access response status, URL, headers, and body separately
Set a timeout to handle slow or unresponsive APIs
Handle failures more efficiently than with raw fetch
What it returns
An object with the following properties:
Property
Description
status
The HTTP status code of the response, for example, 200.
url
The URL of the Fetch API request.
headers
The response headers.
body
The response body in JSON or TEXT format. By default, it is JSON.
The geolocation function enriches events with geolocation data from an IP address. RudderStack uses the MaxMind GeoLite2 database embedded in its service.
The getCredential function retrieves secrets and variables from the RudderStack credential store. Use it to avoid hardcoding sensitive data like API keys and tokens in your transformations.
The credential store and getCredential are available only on the Starter, Growth, and Enterprise plans.
What you can do
Retrieve secrets (encrypted) and variables (non-encrypted) from the credential store
Use API keys, tokens, and configuration values securely in transformations
Reference credentials by name without exposing them in transformation code
What it returns
Returns the credential value as a string when the key exists and is valid
Returns undefined (JavaScript) or null (Python) when the key does not exist or is invalid
getCredential is a restricted keyword. Do not use it for naming functions or variables. You cannot use credentials in transformation libraries. RudderStack drops the event if an error occurs while using getCredential in a transformation connected to a destination.
The log function captures output during transformation testing. Use it to inspect event data, metadata, or intermediate values when you run tests in the RudderStack dashboard.
What you can do
Debug transformations by printing values to the Logs section
Inspect event properties, metadata, or computed values during development
Trace execution flow when troubleshooting transformation logic
What it returns
Nothing. The function is used for its side effect of writing to the transformation logs.
You can pass a string, number, or object as an argument to the log function. Logs appear in the Logs section when you click Run Test in the transformation editor.
Examples
exportfunctiontransformEvent(event,metadata){constmeta=metadata(event);log("Event Name is",event.event,";","Message ID is",event.messageId);log("Source ID is",meta.sourceId);returnevent;}
deftransformEvent(event,metadata):meta=metadata(event)log("Event Name is",event['event'],";","Message ID is",event['messageId'])log("Source ID is",meta['sourceId'])returnevent
This site uses cookies to improve your experience while you navigate through the website. Out of
these
cookies, the cookies that are categorized as necessary are stored on your browser as they are as
essential
for the working of basic functionalities of the website. We also use third-party cookies that
help
us
analyze and understand how you use this website. These cookies will be stored in your browser
only
with
your
consent. You also have the option to opt-out of these cookies. But opting out of some of these
cookies
may
have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This
category only includes cookies that ensures basic functionalities and security
features of the website. These cookies do not store any personal information.
This site uses cookies to improve your experience. If you want to
learn more about cookies and why we use them, visit our cookie
policy. We'll assume you're ok with this, but you can opt-out if you wish Cookie Settings.