Create Function

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

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

{"code":"// Learn more about source functions API at\n// https://segment.com/docs/connections/sources/source-functions\n\n/**\n * Handle incoming HTTP request\n *\n * @param  {FunctionRequest} request\n * @param  {FunctionSettings} settings\n */\nasync function onRequest(request, settings) {\n\tconst body = request.json();\n\tconsole.log(body);\n\n\t// See https://segment.com/docs/connections/spec/track/\n\tSegment.track({\n\t\tevent: 'Test Event',\n\t\tuserId: 'user_id',\n\t\tproperties: {\n\t\t\ttestProperty: 'testValue'\n\t\t}\n\t});\n\n\t// See https://segment.com/docs/connections/spec/identify/\n\tSegment.identify({\n\t\tuserId: 'user_id',\n\t\ttraits: {\n\t\t\tuserName: 'Unicorn'\n\t\t}\n\t});\n\n\t// See https://segment.com/docs/connections/spec/group/\n\tSegment.group({\n\t\tgroupId: 'group_id',\n\t\tuserId: 'user_id',\n\t\ttraits: {\n\t\t\tgroupName: 'Unicorn'\n\t\t}\n\t});\n\n\t// See https://segment.com/docs/connections/spec/page/\n\tSegment.page({\n\t\tname: 'Home page',\n\t\tuserId: 'user_id',\n\t\tproperties: {\n\t\t\turl: 'https://mywebsite.com/about'\n\t\t}\n\t});\n\n\t// See https://segment.com/docs/connections/spec/screen/\n\tSegment.screen({\n\t\tname: 'Test Screen',\n\t\tuserId: 'user_id',\n\t\tproperties: {\n\t\t\turl: 'https://mywebsite.com/about'\n\t\t}\n\t});\n\n\t// See https://segment.com/docs/connections/sources/catalog/libraries/server/object-api/\n\tSegment.set({\n\t\tcollection: 'users',\n\t\tid: 'user_id',\n\t\tproperties: {\n\t\t\tuserName: 'Unicorn'\n\t\t}\n\t});\n}\n","deployed_at":"2020-06-02T21:04:00Z","settings":[{"name":"apiKey","label":"api key","type":"string"},{"name":"mySecret","label":"my secret key","type":"string","sensitive":true}],"id":"sfnc_1234","workspace_id":"workspace_id","display_name":"my test source function","created_at":"2020-07-30T19:15:26.660Z","created_by":"user_id","logo_url":"https://logo-url","type":"SOURCE","catalog_id":"1234"}