title | redirect_from | integration_type | |||
---|---|---|---|---|---|
Source Functions |
|
feature |
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.
info "" Functions is available to all customer plan types with a free allotment of usage hours. Read more about Functions usage limits, or see your workspace's Functions usage stats{:target="_blank"}.
- From your workspace, go to Connections > Catalog and click the Functions tab{:target="_blank"}.
- Click New Function.
- 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 "" Tip: Want to see some example functions? Check out the templates available in the Functions UI, or in the open-source Segment Functions Library{:target="_blank"}. (Contributions welcome!)
Source functions must have an onRequest()
function defined.
This function is executed by Segment for each HTTPS request sent to this function's webhook.
async function onRequest(request, settings) {
// Process incoming data
}
The onRequest()
function receives two arguments:
request
- an object describing the incoming HTTPS request.settings
- set of settings for this function.
To parse the JSON body of the request, use the request.json()
method, as in the following example:
async function onRequest(request) {
const body = request.json()
console.log('Hello', body.name)
}
Use the request.headers
object to get values of request headers.
Since it's an instance of Headers
{:target="_blank"}, the API is the same in both the browser and in Node.js.
async function onRequest(request) {
const contentType = request.headers.get('Content-Type')
const authorization = request.headers.get('Authorization')
}
To access the URL details, refer to request.url
object, which is an instance of URL
{:target="_blank"}.
async function onRequest(request) {
// Access a query parameter (e.g. `?name=Jane`)
const name = request.url.searchParams.get('name')
}
You can send messages to the Segment API using the Segment
object:
async function onRequest(request) {
Segment.identify({
userId: 'user_id',
traits: {
name: 'Jane Hopper'
}
})
Segment.track({
event: 'Page Viewed',
userId: 'user_id',
properties: {
page_name: 'Summer Collection 2020'
}
})
Segment.group({
groupId: 'group_id',
traits: {
name: 'Clearbit'
}
})
Segment.set({
collection: 'products',
id: 'product_id',
properties: {
name: 'Nike Air Max'
}
})
}
Use Identify calls to connect users with their actions, and to record traits about them.
Segment.identify({
userId: 'user_id',
traits: {
name: 'Jane Hopper'
}
})
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, likename
oremail
.context
- Object with extra information that provides useful context, likelocale
orcountry
.
Track calls record actions that users perform, along with any properties that describe the action.
Segment.track({
event: 'Page Viewed',
userId: 'user_id',
properties: {
page_name: 'Summer Collection 2020'
}
})
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, likeproduct_name
orprice
.context
- Object with extra information that provides useful context, likelocale
orcountry
.
Group calls associate users with a group, like a company, organization, account, project, or team.
Segment.group({
groupId: 'group_id',
traits: {
name: 'Clearbit'
}
})
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, likegroup_name
orteam_name
.context
- Object with extra information that provides useful context, likelocale
orcountry
.
Page calls record whenever a user sees a page of your website, along with any other properties about the page.
Segment.page({
name: 'Shoe Catalog',
properties: {
url: 'https://myshoeshop.com/catalog'
}
})
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, likepage_name
orpage_url
.context
- Object with extra information that provides useful context, likelocale
orcountry
.
Screen calls record when a user sees a screen, the mobile equivalent of Page, in your mobile app.
Segment.screen({
name: 'Shoe Feed',
properties: {
feed_items: 5
}
})
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, likescreen_name
.context
- Object with extra information that provides useful context, likelocale
orcountry
.
The Alias call merges two user identities, effectively connecting two sets of user data as one.
Segment.alias({
previousId: '[email protected]',
userId: '[email protected]'
})
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.
Segment.set({
collection: 'products',
id: 'product_id',
properties: {
name: 'Nike Air Max 90',
size: 11
}
})
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 "" 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()
.
{% include content/functions/runtime.md %}
{% include content/functions/settings.md %}
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.
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:
async function onRequest(request, settings) {
const apiKey = settings.apiKey
//=> "super_secret_string"
}
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.
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:
- Generate a public API token{:target="_blank"}.
- Create a Public API Token, or follow these steps:
In your Segment Workspace, navigate to Settings → Workspace settings → Access Management → Token. Click
+ Create Token
. Create a description for the token and assign access. ClickCreate
and save the access token before clickingDone
. - For POST calls, use this Public API token in the Authorization Header, as
Bearer Token : public_api_token
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:
- Send a
POST
request to the source function's provided endpoint (webhook URL) - Include an event
body
- The request must include these Headers:
Content-Type : application/json
orContent-Type : application/x-www-form-urlencoded
Authorization : Bearer _your_public_api_token_
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
orContent-Type : application/x-www-form-urlencoded
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.
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.
Functions can throw an Error or custom Error{:target="_blank"}, and you can also add additional helpful context in logs using the console
API{:target="_blank"}.
For example:
async function onRequest(request, settings) {
const body = request.json()
const userId = body.userId
console.log('User ID is', userId)
if (typeof userId !== 'string' || userId.length < 8) {
throw new Error('User ID is invalid')
}
console.log('User ID is valid')
}
warning "" Warning: 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.
- Bad Request: is any error thrown by your code not covered by the other errors.
- Invalid Settings: A configuration error prevented Segment from executing your code. If this error persists for more than an hour, contact Segment Support{:target="_blank"}.
- Message Rejected: Your code threw
InvalidEventPayload
orValidationError
due to invalid input. - Unsupported Event Type: Your code doesn't implement a specific event type (for example,
onTrack()
) or threw anEventNotSupported
error. - StatusCode: 429, TooManyRequestsException: Rate Exceeded: Rate limit exceeded. These events will be retried when the rate becomes available.
- failed calling Tracking API: the message is too large and over the maximum 32KB limit: Segment's Tracking API can only handle API requests that are 32KB or smaller. Reduce the size of the request for Segment to accept the event.
- Retry: Your code threw
RetryError
indicating that the function should be retried.
Segment only attempts to run your source function again if a Retry error occurs.
{% include content/functions/perms.md%}
If you are a Workspace Owner or Functions Admin, you can manage your source function from the Functions{:target="_blank"} tab in the catalog.
info "" You must be a Workspace Owner or Source Admin to connect an instance of your function in your workspace.
From the Functions tab{:target="_blank"}, 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.
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.
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.
The maximum payload size for an incoming webhook payload is 512 KiB.
The execution time limit is five seconds, however Segment strongly recommends that you keep execution time as low as possible. If you are making multiple external requests you can use async / await to make them concurrently, which will help keep your execution time low.
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.
GET
requests are not supported with a source function. Source functions can only receive data through POST
requests.
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.
The test function interface has a 4KB console logging limit. Outputs surpassing this limit will not be visible in the user interface.
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?
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.