Preview Function

POST {{host}}/{{version}}/{{workspace_id}}/functions/preview

Preview is used to test function code before updating and deploying.

Preview Input

FieldTypeDescription
workspace_idstringMetrics enabled for the sourceoptional
functionFunction ModelThe complete functions object.required
typestringType of function: SOURCE or DESTINATIONrequired
payloadstringJSON payload to invoke the functionrequired

Example payload (Source Function)

{
  "payload": {
    "body": {"foo":"bar"},
    "headers": {
      "Accept": ["*/*"],
      "Content-Length": ["13"],
      "Content-Type": ["application/json"],
      "User-Agent": ["curl/7.64.1"],
      "X-Request-Id": ["64bcb940-8071-473c-8836-10d4f42b7a24"]
    },
    "queryParameters": {}
  }
}

Example payload (Destination Function)

{
  "type": "track",
  "event": "Test event",
  "userId": "foo@example.com",
  "timestamp": "2019-11-15T14:26:36-08:00",
  "properties": {}

Example response { "invoke": { "success": true, "output": "{ "events": [{ "type": "track", "event": "Custom Source 1 Executed", "userId": "1234" }], "objects": [] }", "trace": { "source": "preview-1234", "version": "1234", "specversion": "1.0", "type": "com.segment.event.ack", "time": "2020-07-31T22:16:25.553Z", "trace": { "name": "invoke", "time": "2020-07-31T22:16:25.553Z", "duration": "0.201s", "logs": [{ "time": "2020-07-31T22:16:25.653Z", "key": "NOTICE", "value": "{ foo: 'bar' }" }] } } }, "logs": [{ "message": "{ foo: 'bar' }", "created_at": "2020-07-31T22:16:25.656Z" }], "session_id": "1234" }

Request Body

{"function"=>{"code"=>"// Learn more about source functions API at\r\n// https://segment.com/docs/connections/sources/source-functions\r\n\r\n/**\r\n * Handle incoming HTTP request\r\n *\r\n * @param  {FunctionRequest} request\r\n * @param  {FunctionSettings} settings\r\n */\r\nasync function onRequest(request, settings) {\r\n  const body = request.json()\r\n\r\n  const endpoint = ''; // replace with your endpoint\r\n\tconst response = await fetch(endpoint, {\r\n\t\tmethod: 'POST',\r\n\t\theaders: {\r\n\t\t\tAuthorization: `Basic ${btoa(settings.apiKey + ':')}`,\r\n\t\t\t'Content-Type': 'application/json'\r\n\t\t},\r\n\t\tbody: JSON.stringify(body)\r\n  });\r\n\r\n\tif (!response.ok) {\r\n    console.log(\"ERROR fetching information: \", response)\r\n    return;\r\n  }\r\n\r\n  // See https://segment.com/docs/connections/spec/track/\r\n  Segment.track({\r\n    event: 'Test Event',\r\n    userId: 'user_id',\r\n    properties: {\r\n      testProperty: 'testValue',\r\n      testProperty2: response.propertyName,\r\n    }\r\n  })\r\n\r\n  // See https://segment.com/docs/connections/spec/identify/\r\n  Segment.identify({\r\n    userId: 'user_id',\r\n    traits: {\r\n      userName: 'Unicorn'\r\n    }\r\n  })\r\n\r\n  // See https://segment.com/docs/connections/spec/group/\r\n  Segment.group({\r\n    groupId: 'group_id',\r\n    userId: 'user_id',\r\n    traits: {\r\n      groupName: 'Unicorn'\r\n    }\r\n  })\r\n\r\n  // See https://segment.com/docs/connections/spec/page/\r\n  Segment.page({\r\n    name: 'Home page',\r\n    userId: 'user_id',\r\n    properties: {\r\n      url: 'https://mywebsite.com/about'\r\n    }\r\n  })\r\n\r\n  // See https://segment.com/docs/connections/spec/screen/\r\n  Segment.screen({\r\n    name: 'Test Screen',\r\n    userId: 'user_id',\r\n    properties: {\r\n      url: 'https://mywebsite.com/about'\r\n    }\r\n  })\r\n\r\n  // See https://segment.com/docs/connections/sources/catalog/libraries/server/object-api/\r\n  Segment.set({\r\n    collection: 'users',\r\n    id: 'user_id',\r\n    properties: {\r\n      userName: 'Unicorn'\r\n    }\r\n  })\r\n}", "settings"=>[{"name"=>"myapiKey", "label"=>"my api key", "type"=>"string", "required"=>"false", "sensitive"=>"false"}, {"name"=>"mySecret", "label"=>"my secret key", "type"=>"string", "required"=>"false", "sensitive"=>"true"}], "display_name"=>"test source function"}, "type"=>"SOURCE"}

HEADERS

KeyDatatypeRequiredDescription
Content-Typestring

RESPONSES

status: OK

{"invoke":{"success":true,"output":"{\"events\":[{\"event\":\"Test Event\",\"userId\":\"user_id\",\"properties\":{\"testProperty\":\"testValue\"},\"type\":\"track\"},{\"userId\":\"user_id\",\"traits\":{\"userName\":\"Unicorn\"},\"type\":\"identify\"},{\"groupId\":\"group_id\",\"userId\":\"user_id\",\"traits\":{\"groupName\":\"Unicorn\"},\"type\":\"group\"},{\"name\":\"Home page\",\"userId\":\"user_id\",\"properties\":{\"url\":\"https://mywebsite.com/about\"},\"type\":\"page\"},{\"name\":\"Test Screen\",\"userId\":\"user_id\",\"properties\":{\"url\":\"https://mywebsite.com/about\"},\"type\":\"screen\"}],\"objects\":[{\"collection\":\"users\",\"id\":\"user_id\",\"properties\":{\"userName\":\"Unicorn\"}}]}","trace":{"source":"preview-12324","version":"1234","specversion":"1.0","type":"com.segment.event.ack","time":"2020-07-31T22:16:25.553Z","trace":{"name":"invoke","time":"2020-07-31T22:16:25.553Z","duration":"0.201s","logs":[{"time":"2020-07-31T22:16:25.653Z","key":"NOTICE","value":"{ foo: 'bar' }"}]}}},"logs":[{"message":"{ foo: 'bar' }","created_at":"2020-07-31T22:16:25.656Z"}],"session_id":"123456"}