Skip to contentSkip to navigationSkip to topbar
On this page
Looking for more inspiration?Visit the
(information)
You're in the right place! Segment documentation is now part of Twilio Docs. The content you are used to is still here—just in a new home with a refreshed look.

Source Functions


Source Functions allow you to gather data from any third-party applications without worrying about setting up or maintaining any infrastructure.

All functions are scoped to your workspace, so members of other workspaces cannot view or use them.

(information)

Info

A graphic illustrating Segment source functions.

Create a source function

create-a-source-function page anchor
  1. From your workspace, go to Connections > Catalog and click the Functions tab(link takes you to an external page).
  2. Click New Function.
  3. Select Source as the function type and click Build.

After you click Build, a code editor appears. Use the editor to write the code for your function, configure settings, and test the function's behavior.

(success)

Want to see some example functions?

Check out the templates available in the Functions UI, or in the open-source Segment Functions Library(link takes you to an external page). (Contributions welcome!)

Functions Editor.

Code the source function

code-the-source-function page anchor

Source functions must have an onRequest() function defined. This function is executed by Segment for each HTTPS request sent to this function's webhook.

1
async function onRequest(request, settings) {
2
// Process incoming data
3
}

The onRequest() function receives two arguments:

  • request - an object describing the incoming HTTPS request.
  • settings - set of settings for this function.

Request processing

request-processing page anchor

To parse the JSON body of the request, use the request.json() method, as in the following example:

1
async function onRequest(request) {
2
const body = request.json()
3
console.log('Hello', body.name)
4
}

Use the request.headers object to get values of request headers. Since it's an instance of Headers(link takes you to an external page), the API is the same in both the browser and in Node.js.

1
async function onRequest(request) {
2
const contentType = request.headers.get('Content-Type')
3
const authorization = request.headers.get('Authorization')
4
}

To access the URL details, refer to request.url object, which is an instance of URL(link takes you to an external page).

1
async function onRequest(request) {
2
// Access a query parameter (e.g. `?name=Jane`)
3
const name = request.url.searchParams.get('name')
4
}

Sending messages

sending-messages page anchor

You can send messages to the Segment API using the Segment object:

1
async function onRequest(request) {
2
Segment.identify({
3
userId: 'user_id',
4
traits: {
5
name: 'Jane Hopper'
6
}
7
})
8
9
Segment.track({
10
event: 'Page Viewed',
11
userId: 'user_id',
12
properties: {
13
page_name: 'Summer Collection 2020'
14
}
15
})
16
17
Segment.group({
18
groupId: 'group_id',
19
traits: {
20
name: 'Clearbit'
21
}
22
})
23
24
Segment.set({
25
collection: 'products',
26
id: 'product_id',
27
properties: {
28
name: 'Nike Air Max'
29
}
30
})
31
}
Identify
identify page anchor

Use Identify calls to connect users with their actions, and to record traits about them.

1
Segment.identify({
2
userId: 'user_id',
3
traits: {
4
name: 'Jane Hopper'
5
}
6
})

The Segment.identify() method accepts an object with the following fields:

  • userId - Unique identifier for the user in your database.
  • anonymousId - A pseudo-unique substitute for a User ID, for cases when you don't have an absolutely unique identifier.
  • traits - Object with data about or related to the user, like name or email.
  • context - Object with extra information that provides useful context, like locale or country.

Track calls record actions that users perform, along with any properties that describe the action.

1
Segment.track({
2
event: 'Page Viewed',
3
userId: 'user_id',
4
properties: {
5
page_name: 'Summer Collection 2020'
6
}
7
})

The Segment.track() method accepts an object with the following fields:

  • userId - Unique identifier for the user in your database.
  • anonymousId - A pseudo-unique substitute for a User ID, for cases when you don't have an absolutely unique identifier.
  • properties - Object with data that is relevant to the action, like product_name or price.
  • context - Object with extra information that provides useful context, like locale or country.

Group calls associate users with a group, like a company, organization, account, project, or team.

1
Segment.group({
2
groupId: 'group_id',
3
traits: {
4
name: 'Clearbit'
5
}
6
})

The Segment.group() method accepts an object with the following fields:

  • groupId - Unique identifier for the group in your database.
  • traits - Object with data that is relevant to the group, like group_name or team_name.
  • context - Object with extra information that provides useful context, like locale or country.

Page calls record whenever a user sees a page of your website, along with any other properties about the page.

1
Segment.page({
2
name: 'Shoe Catalog',
3
properties: {
4
url: 'https://myshoeshop.com/catalog'
5
}
6
})

The Segment.page() method accepts an object with the following fields:

  • userId - Unique identifier for the user in your database.
  • anonymousId - A pseudo-unique substitute for a User ID, for cases when you don't have an absolutely unique identifier.
  • name - Name of the page.
  • properties - Object with information about the page, like page_name or page_url.
  • context - Object with extra information that provides useful context, like locale or country.

Screen calls record when a user sees a screen, the mobile equivalent of Page, in your mobile app.

1
Segment.screen({
2
name: 'Shoe Feed',
3
properties: {
4
feed_items: 5
5
}
6
})

The Segment.screen() method accepts an object with the following fields:

  • userId - Unique identifier for the user in your database.
  • anonymousId - A pseudo-unique substitute for a User ID, for cases when you don't have an absolutely unique identifier.
  • name - Name of the screen.
  • properties - Object with data about the screen, like screen_name.
  • context - Object with extra information that provides useful context, like locale or country.

The Alias call merges two user identities, effectively connecting two sets of user data as one.

1
Segment.alias({
2
previousId: 'old-email@example.com',
3
userId: 'new-email@example.com'
4
})

The Segment.alias() method accepts an object with the following fields:

  • previousId - Previous unique identifier for the user.
  • userId - Unique identifier for the user in your database.
  • anonymousId - A pseudo-unique substitute for a User ID, for cases when you don't have an absolutely unique identifier.

The Set call uses the object API to save object data to your Redshift, BigQuery, Snowflake, or other data warehouses supported by Segment.

1
Segment.set({
2
collection: 'products',
3
id: 'product_id',
4
properties: {
5
name: 'Nike Air Max 90',
6
size: 11
7
}
8
})

The Segment.set() method accepts an object with the following fields:

  • collection - A collection name, which must be lowercase.
  • id - An object's unique identifier.
  • properties - An object with free-form data.
(warning)

Warning

When you use the set() method, you won't see events in the Source Debugger. Segment only sends events to connected warehouses.

Declare settings variables in the function handler, rather than globally in your function. This prevents you from leaking the settings values across other function instances.

The handler for source functions is onRequest().

Runtime and dependencies

runtime-and-dependencies page anchor

Segment Functions run on the Node.js LTS runtime (currently v20). This keeps the runtime current with industry standards and security updates.

Based on the AWS Lambda(link takes you to an external page) and Node.js(link takes you to an external page) support schedule, production applications should only use Node.js releases that are in Active LTS or Maintenance LTS.

When Segment upgrades the Functions runtime to a new LTS version, existing functions automatically use the new runtime after their next deployment. Segment recommends checking your function after deployment to ensure everything works as expected, since dependency or syntax changes between Node.js versions might affect your function's behavior.

Functions don't support importing dependencies, but you can contact Segment Support(link takes you to an external page) to request that one be added.

The following dependencies are installed in the function environment by default:

The following Node.js modules are available:

Other built-in Node.js modules(link takes you to an external page) aren't available.

For more information on using the aws-sdk module, see how to set up functions for calling AWS APIs.

Basic cache storage is available through the cache object, which has the following methods defined:

  • cache.load(key: string, ttl: number, fn: async () => any): Promise<any>
    • Obtains a cached value for the provided key, invoking the callback if the value is missing or has expired. The ttl is the maximum duration in milliseconds the value can be cached. If omitted or set to -1, the value will have no expiry.
  • cache.delete(key: string): void
    • Immediately remove the value associated with the key.

Some important notes about the cache:

  • When testing functions in the code editor, the cache will be empty because each test temporarily deploys a new instance of the function.
  • Values in the cache are not shared between concurrently-running function instances; they are process-local which means that high-volume functions will have many separate caches.
  • Values may be expunged at any time, even before the configured TTL is reached. This can happen due to memory pressure or normal scaling activity. Minimizing the size of cached values can improve your hit/miss ratio.
  • Functions that receive a low volume of traffic may be temporarily suspended, during which their caches will be emptied. In general, caches are best used for high-volume functions and with long TTLs. The following example gets a JSON value through the cache, only invoking the callback as needed:
1
const ttl = 5 * 60 * 1000 // 5 minutes
2
const val = await cache.load("mycachekey", ttl, async () => {
3
const res = await fetch("http://echo.jsontest.com/key/value/one/two")
4
const data = await res.json()
5
return data
6
})

Create settings and secrets

create-settings-and-secrets page anchor

Settings allow you to pass configurable variables to your function, which is the best way to pass sensitive information such as security tokens. For example, you might use settings as placeholders to use information such as an API endpoint and API key. This way, you can use the same code with different settings for different purposes. When you deploy a function in your workspace, you are prompted to fill out these settings to configure the function.

First, add a setting in Settings tab in the code editor:

A screenshot of the functions settings tab.

Click Add Setting to add your new setting.

A screenshot of the 'Add Setting' section of the functions settings tab, with apiKey settings included.

You can configure the details about this setting, which change how it's displayed to anyone using your function:

  • Label - Name of the setting, which users see when configuring the function.
  • Name - Auto-generated name of the setting to use in function's source code.
  • Type - Type of the setting's value.
  • Description - Optional description, which appears below the setting name.
  • Required - Enable this to ensure that the setting cannot be saved without a value.
  • Encrypted - Enable to encrypt the value of this setting. Use this setting for sensitive data, like API keys.

As you change the values, a preview to the right updates to show how your setting will look and work.

Click Add Setting to save the new setting.

Once you save a setting, it appears in the Settings tab for the function. You can edit or delete settings from this tab.

A screenshot of the functions settings tab, showing the apiKey setting.

Next, fill out this setting's value in Test tab, so that you can run the function and check the setting values being passed.

Note, this value is only for testing your function.

Test Value For Setting.

Now that you've configured a setting and filled in a test value, you can add code to read its value and run the function:

1
async function onRequest(request, settings) {
2
const apiKey = settings.apiKey
3
//=> "super_secret_string"
4
}

When you deploy a source function in your workspace, you are prompted to fill out settings to configure the source. You can access these settings later by navigating to the Source Settings page for the source function.

Source Function Settings.

Test the source function

test-the-source-function page anchor

You can test your code directly from the editor in two ways: either by receiving real HTTPS requests through a webhook, or by manually constructing an HTTPS request from within the editor.

The advantage of testing your source function with webhooks is that all incoming data is real, so you can test behavior while closely mimicking the production conditions.

Note: Segment has updated the webhook URL to api.segmentapis.com/functions. To use webhooks with your function, you must:

Testing source functions with a webhook

testing-source-functions-with-a-webhook page anchor

You can use webhooks to test the source function either by sending requests manually (using any HTTP client such as cURL, Postman, or Insomnia), or by pasting the webhook into an external server that supports webhooks (such as Slack).

A common Segment use case is to connect a Segment webhooks destination or webhook actions destination to a test source, where the Webhook URL/endpoint that is used corresponds to the provided source function's endpoint, then you can trigger test events to send directly to that source, which are routed through your Webhook destination and continue on to the source function: Source > Webhook destination > Source Function.

From the source function editor, copy the provided webhook URL (endpoint) from the "Auto-fill via Webhook" dialog.

Note : When a new source is created that utilizes a source function, the new source's endpoint (webhook URL) will differ from the URL that is provided in the source function's test environment.

To test the source function:

  1. Send a POST request to the source function's provided endpoint (webhook URL)
  2. Include an event body
  3. The request must include these Headers:
  • Content-Type : application/json or Content-Type : application/x-www-form-urlencoded
  • Authorization : Bearer _your_public_api_token_

Testing source functions manually

testing-source-functions-manually page anchor

You can also manually construct the headers and body of an HTTPS request inside the editor and test with this data without using webhooks. The Content-Type Header is required when testing the function:

  • Content-Type : application/json or Content-Type : application/x-www-form-urlencoded
Test HTTPS Request.

Save and deploy the function

save-and-deploy-the-function page anchor

After you finish building your source function, click Configure to name it, then click Create Function to save it. The source function appears on the Functions page in your workspace's catalog.

If you're editing an existing function, you can Save changes without updating instances of the function that are already deployed and running.

You can also choose to Save & Deploy to save the changes, and then choose which already-deployed functions to update with your changes. You might need additional permissions to update existing functions.


Source functions logs and errors

source-functions-logs-and-errors page anchor

Your function may encounter errors that you missed during testing, or you might intentionally throw errors in your code (for example, if the incoming request is missing required fields).

If your function throws an error, execution halts immediately. Segment captures the incoming request, any console logs the function printed, and the error, and displays this information in the function's Errors tab. You can use this tab to find and fix unexpected errors.

Source Function Error Logs.

Functions can throw an Error or custom Error(link takes you to an external page), and you can also add additional helpful context in logs using the console API(link takes you to an external page). For example:

1
async function onRequest(request, settings) {
2
const body = request.json()
3
const userId = body.userId
4
5
console.log('User ID is', userId)
6
7
if (typeof userId !== 'string' || userId.length < 8) {
8
throw new Error('User ID is invalid')
9
}
10
11
console.log('User ID is valid')
12
}
(warning)

Data may be visible to other workspace members

Do not log sensitive data, such as personally-identifying information (PII), authentication tokens, or other secrets. You should especially avoid logging entire request/response payloads.

Segment only retains the 100 most recent errors and logs for up to 30 days but the Errors tab may be visible to other workspace members if they have the necessary permissions.

Segment only attempts to run your source function again if a Retry error occurs.


Managing source functions

managing-source-functions page anchor

Source functions permissions

source-functions-permissions page anchor

Functions have specific roles which can be used for access management in your Segment workspace.

Access to functions is controlled by two permissions roles:

  • Functions Admin: Create, edit, and delete all functions, or a subset of specified functions.
  • Functions Read-only: View all functions, or a subset of specified functions.

You also need additional Source Admin permissions to enable source functions, connect destination functions to a source, or to deploy changes to existing functions.

Editing and deleting source functions

editing-and-deleting-source-functions page anchor

If you are a Workspace Owner or Functions Admin, you can manage your source function from the Functions(link takes you to an external page) tab in the catalog.

Connecting source functions

connecting-source-functions page anchor
(information)

Info

You must be a Workspace Owner or Source Admin to connect an instance of your function in your workspace.

From the Functions tab(link takes you to an external page), click Connect Source and follow the prompts to set it up in your workspace.

After configuring, find the webhook URL - either on the Overview or Settings → Endpoint page.

Copy and paste this URL into the upstream tool or service to send data to this source.


What is the retry policy for a webhook payload?
what-is-the-retry-policy-for-a-webhook-payload page anchor

Segment retries invocations that throw RetryError or Timeout errors up to six times. After six attempts, the request is dropped. The initial wait time for the retried event is a random value between one and three minutes. Wait time increases exponentially after every retry attempt. The maximum wait time between attempts can reach 20 minutes.

I configured RetryError in a function, but it doesn't appear in my source function error log.
i-configured-retryerror-in-a-function-but-it-doesnt-appear-in-my-source-function-error-log page anchor

Retry errors only appear in the source function error logs if the event has exhausted all six retry attempts and, as a result, has been dropped.

What is the maximum payload size for the incoming webhook?
what-is-the-maximum-payload-size-for-the-incoming-webhook page anchor

The maximum payload size for an incoming webhook payload is 512 KiB.

What is the timeout for a function to execute?
what-is-the-timeout-for-a-function-to-execute page anchor

The execution time limit is five seconds, however we strongly recommend that you keep execution time as low as possible. If you are making multiple external requests you can use async or await to make them concurrently, which will help keep your execution time low.

Does Segment alter incoming payloads?

does-segment-alter-incoming-payloads page anchor

Segment alphabetizes payload fields that come in to deployed source functions. Segment doesn't alphabetize payloads in the Functions tester. If you need to verify the exact payload that hits a source function, alphabetize it first. You can then make sure it matches what the source function ingests.

Does the source function allow GET requests?

does-the-source-function-allow-get-requests page anchor

GET requests are not supported with a source function. Source functions can only receive data through POST requests.

Can I use a Source Function in place of adding a Tracking Pixel to my code?

can-i-use-a-source-function-in-place-of-adding-a-tracking-pixel-to-my-code page anchor

No. Tracking Pixels operate client-side only and need to be loaded onto your website directly. Source functions operate server-side only, and aren't able to capture or implement client-side tracking code. If the tool you're hoping to integrate is server-side, then you can use a Source function to connect it to Segment.

What is the maximum data size that can be displayed in console.logs() when testing a Function?
what-is-the-maximum-data-size-that-can-be-displayed-in-consolelogs-when-testing-a-function page anchor

The test function interface has a 4KB console logging limit. Outputs surpassing this limit will not be visible in the user interface.

Can I send a custom response from my source function to an external tool?

can-i-send-a-custom-response-from-my-source-function-to-an-external-tool page anchor

No, Source functions can't send custom responses to the tool that triggered the Function's webhook. Source functions can only send a success or failure response, not a custom one.

Why am I seeing the error "Functions are unable to send data or events back to their originating source" when trying to save my Source Function?

why-am-i-seeing-the-error-functions-are-unable-to-send-data-or-events-back-to-their-originating-source-when-trying-to-save-my-source-function page anchor

This error occurs because Segment prevents Source functions from sending data back to their own webhook endpoint (https://fn.segmentapis.com). Allowing this could create an infinite loop where the function continuously triggers itself.

To resolve this error, check your Function code and ensure the URL https://fn.segmentapis.com is not included. This URL is used to send data to a Source Function and shouldn't appear in your outgoing requests. Once you remove this URL from your code, you'll be able to save the Function successfully.