Authentication
Most destinations require some sort of authentication. Segment's destination interface provides details about how customers need to authenticate with your destination to send data or retrieve data for dynamic input fields.
Basic authentication is useful if your destination requires a username and password to authenticate. These are values that only the customer and the destination know.
Success!
When scaffolding your integration, select Basic Auth from the auto-prompt or pass --template basic-auth.
1const authentication = {2// the 'basic' authentication scheme tells Segment to automatically3// include the `username` and `password` fields so you don't have to.4// Segment will automatically do base64 header encoding of the username:password5scheme: 'basic',67fields: {8username: {9label: 'Username',10description: 'Your username',11type: 'string',12required: true13},14password: {15label: 'password',16description: 'Your password.',17type: 'string',18required: true19}20},2122// a function that can test the user's credentials23testRequest: (request) => {24return request('https://example.com/api/accounts/me.json')25}26}2728const destination = {29// ...other properties30authentication,3132extendRequest({ settings }) {33return {34username: settings.username,35password: settings.password36}37}38}
Custom authentication is the most commonly used authentication among Segment destinations. It's what most "API Key" based authentication should use. You'll likely need to define an extendRequest function to complete the authentication by modifying request headers with some authentication input fields.
1const authentication = {2// the 'custom' scheme doesn't do anything automagically for you, but let's you3// define the behavior through input fields and `extendRequest`.4// this is what most API key-based destinations should use5scheme: 'custom',67// a function that can test the user's credentials8testRequest: (request) => {9return request(`/accounts/me.json`)10},1112// fields that are specific to authentication13fields: {14subdomain: {15type: 'string',16label: 'Subdomain',17description: 'The subdomain for your account, found in your user settings.',18required: true19},20apiKey: {21type: 'string',22label: 'API Key',23description: 'Found on your settings page.',24required: true25}26}27}2829const destination = {30// ...other properties31authentication,32// we may explore a simple JSON representation that supports template strings33extendRequest: ({ settings }) => {34return {35prefixUrl: `https://${settings.subdomain}.example.com/api`,36headers: { Authorization: `Bearer ${settings.apiKey}` },37responseType: 'json'38}39}40}
If your API supports OAuth 2.0, you can authenticate your destination's users with it.
1const authentication = {2// the 'oauth-managed' authentication scheme tells Segment use the3// oauth credentials and endpoint that you provide to authenticate users.4scheme: 'oauth-managed',5}
When you receive access to the Developer Portal, find your integration, and navigate to the OAuth settings tab to configure the integration's OAuth details.