Up to this point weve ignored something that is usually crucial to APIs: authentication. Second, look at the second part of our snippet; the export. Zapier implements the Authorization Code grant type when you choose OAuth v2. Read the Zapier blog for tips on productivity, automation, and growing your business. If your Access Token and Refresh Token requests dont return the tokens at the top level, use Code Mode to modify the response so that the tokens are available at the top level. All you need to add is a test API call where Zapier can verify that the credentials work, and optionally a connection label to help users identify the account. Youll want to keep that file safe and not check it into source control. If you need further customization of your login flow or need additional data from users, API key authentication may be what your API requires. It's very important that you save your 10 recovery codes and put them in a safe place separate from your mobile device. Need help? In this tutorial, well walk you through the process of building, testing, and pushing an example app to Zapier. You can now use this GitHub account to test triggers and actions as you build them into your integration. Zapier does not log authentication credentials in plain text. Example API key auth screen for users inside Zapier. Finally add a connection label to help users identify each account that they add from your app to Zapier. Then, test your authentication, adding a real account to ensure Zapier can successfully connect to your app, exchange user credentials for an access or session token, and use your test API call. We can verify the header is present in the request by looking at the logs again. Now that your app version is properly pushed, log in and visit https://zapier.com/app/editor to create a Zap and check our progress. First, add your applications Authorization URL, where Zapier will redirect users to authenticate with your app. In our case, every HTTP request will now have the API key added in a header. Need help? Much like Zapiers input designer for triggers and actions, this lets you design a simple form for users to enter their username, password, API key, domain, or any other data your API requires for authentication. When a user adds an app account to Zapier with Session auth, they first fill out an input form with any authentication credentials that apps API requires. Well use basic auth in this sample GitHub integration to keep things simple. (If you've previously used any of your recovery codes, you may see fewer than 10 displayed.). Click the field to edit it, or click the gear icon and select Delete to remove a field. We highly recommend saving your recovery codes in a safe place separate from your 2FA device. By using 2FA, anyone who wanted to access your account would need both your password and an access code generated on your phone or mobile device. See How does Code Mode work? Session authentication shares elements of Basic authenticationwhere Zapier requests a username and password, then uses them directly to authenticate each API calland OAuth v2where Zapier redirects users to the apps site to allow access, then exchanges credentials for a token it uses to authenticate subsequent API calls. Need help? Enter the same key name that your API uses. It works much like cookie-based authentication in your browser, only here the cookie is an auth token stored by Zapier. If your integration requires OAuth v1 authentication, use the Zapier CLI to build your integration instead. At this point, weve come full circle on the trigger definition from earlier. You can also invite users from the visibility tab of your developer dashboard: For more on inviting users, see our detailed documentation. Zapier then needs a Test API calltypically to a /user or /me endpoint that returns details about the user and needs no additional configurationto test account authentication and ensure the access token works. If you lose your phone or mobile device that you used to authenticate your Zapier account, you will need to use one of the 10 recovery codes for your account. Hire a Zapier Expert to help you improve processes and automate workflows. Enter the 6-digit verification code generated by the authentication app. Your integration defines how Zapiers platform authenticates with the API and what data needs to be collected from users to allow access to their accounts. Well explain later how Zapier uses this metadata and exposes it to the end user. Note: If youre new to promises, they are essentially synchronous callbacks. Basic Auth in Zapier shows a form where users enter their username and passwordunlike OAuth2 above where Zapier sends users to your site to authenticate. Early feedback can help you make sure youre building something theyll use! Its not possible to store an object with nested keys from the response. You can also check out the other example apps to see how to incorporate different authentication schemes into your app and how to implement things like searches and creates. Get help with Zapier from our tutorials, FAQs, and troubleshooting articles. To regain access to your account, follow these steps: If you lose your 2FA device and your recovery codes, you will be permanently locked out of your Zapier account. Save each field after adding it, then click Continue when every field your API needs has been added. In the above snippet, we define the two required properties of authentication: With that setup, we now need to make sure that our API key is included in all the requests our app makes. You will need the following to set up Session Auth: The first thing to add for Session auth is an input form. Tell us about your problem and well connect you with the right resource or contact support. You will receive a text message with a verification code. For GitHub, add {{bundle.authData.username}} to the Connection Label to include users GitHub username along with the app name that Zapier automatically includes. For now, know that it satisfies the minimum info required to define a trigger. If your integration requires a different OAuth 2 grant type, youll need to use another supported authorization type with Zapier such as Session auth or API Key auth. When others use your integration, theyll see a similar login flow to connect their GitHub account to Zapieronly there, the popup window will open from the Zap editor. Read the docs for more info. # Build and upload complete! Add it yourselfhere, and in subsequent Trigger and Action step API callsin your API call settings. To add input fields, click the Add Fields button, then select the type of field you need. With Basic authentication, you only specify an API endpoint to test the users credentials. Now that your CLI is installed - youll need to identify yourself via the CLI. Next, add your apps Access Token Request URL, typically with a POST call and the default settings Zapier uses if your app uses a standard OAuth configuration. Once logged in, you should reconfigure your authentication device to avoid being locked out of your account in the future. Selecting it, youll see the New Recipe trigger. Zapier stores that authentication token for use with subsequent API calls. Whenever someone uses your integration in a Zap, theyll first select your app, then will connect their account. Once they authorize Zapier access, the app will return an access token that Zapier can use to authenticate future API calls. In the dialog box, enter your 2FA authentication code from your authentication app. There is also an index.js file and a test directory (more on those later). Visit the Zapier Community if you need any guidance on using Session Auth to provide an OAuth flow. You can additionally include help text under each field to direct users to where they can retrieve their API key. You should be able to run the test with zapier test and see it pass: For this example, well stick to a single test, but you can see what multiple tests look like in our this example. Click Save & Continue to save your progress so far. Youll need the following items to add OAuth authentication: Note: Most apps with OAuth v2 authentication do not need an input form, so unless your API requires data from the user before contacting the authorization URL, or requires URL details to create the authorization URL, you should likely not include an input form. You can also change your emergency phone number by following the directions above. Add the field key, using the same field key as your APIs response, and leave the remaining field details blank. Click Save & Continue once finished to store your API call settings. Many Zapier integrations use OAuth2 authentication to connect user accounts, including Zapiers built-in GitHub integration. Video courses designed to help you become a better Zapier user. The value appears as :censored:6:b1af149262:, which is intentional. There, add the following details: Be sure to add one field for every piece of data users need to enter to authenticate their account with your API. The name chosen is illustrative, it can be whatever the API you are integrating with requires. Be sure to add the most commonly needed fields first, in the order users expect, as you cannot reorder fields once added. Need help? Zapier will show an error if a field marked as computed is not included in the response data. In triggers/recipe.js, replace the file with: Notice that we now include and use an input field keyed "style". Finally add a connection label to uniquely identify each account users add from your app to Zapier. This lets you add headers, query params, or whatever is needed to be within all outbound requests. How to Add OAuth v2 to a Zapier Integration, Add Access Token Request and Refresh Token Request URLs, An input form (optional) to gather data from users, such as account team name or site URL for self-hosted apps, An OAuth application configured in your own apps settings, where youll add Zapiers OAuth Redirect URL, A Client ID (may be called Customer or API Key) and Client Secret (may be called Customer or API secret) from your app, An Authorization URL on your apps site where users will log in with their account credentials, A list of API scope(s) (optional) to restrict what Zapier can access, An Access Token Request URL where Zapier exchanges the request token for an access token, A Refresh Token Request URL (optional) where Zapier can refresh the access token if it expires, along with an option to have Zapier automatically refresh credentials, A Test API endpoint where Zapier can make an API call to ensure your user credentials work, A Connection Label to uniquely identify users accounts, Plain text that will be included in every account connection, Any input field from your authentication formenter, Output fields from your apps authentication test API call, referenced with. Lets say we want to let our users tweak the cuisine style of recipes they are triggering on. This means that even if someone stole your password, they would be unable to access your account without your mobile device. Go back to your Zap at https://zapier.com. If you ever need to reference Zapiers redirect URL inside your Zapier integration, use the following code: In your applications settings, youll receive credentials that Zapier will use to verify itself to your apptypically called a Client ID and Client Secret, though they may have a slightly different name. Finally, add a connection label to help users identify each account that they add from your app to Zapier. If you need to use data received from the auth API responsesuch as team account names, domains, or subdomainsyou can add a Computed Field by selecting the Field Type at the top of the form. As you click through, youll see our input field style appear, which you can fill out. Above we define a helper function, addApiKeyToHeader, that puts the user-provided API key in a request header called MY-AUTH-HEADER. Those properties control the info you see inside the Zapier UI. When you add Basic Auth to your integration, Zapier adds a pre-built form that requests a username and password whenever users authenticate with your API. Add the endpoint URL to the Test field, setting the correct HTTP method. Every input field requires a Key, the name your API uses to reference this field. Open triggers/recipe.js (file created by zapier init) and replace it with: Lets break down what is happening in this snippet! Copy that data from your app, then in Step 3 of your Zapier OAuth configuration, paste those items in their respective fields. By being members of the same team, you'll be able to continue sharing access to Zaps by using shared folders while still enabling two-factor authentication for each team member. Tell us about your problem and well connect you with the right resource or contact support. Connect with other Zapier users and industry professionals to get help crafting the perfect workflow. Thats where the authentication flow comes in. This ensures you'll still have access to your recovery codes if you lose your 2FA device. To add OAuth authentication to a Zapier integration, open your apps Authentication page in Zapiers visual builder, then select OAuth v2 from the authentication scheme drop-down. Zapier then sends a request to the APIs token exchange endpoint with those credentials, and the API responds with an authentication token. beforeRequest is a list of functions that are called before every HTTP request that uses z.request or the default perform function. Learn about automation anytime, anywhere with our on-demand webinar library. To add Session Auth to a Zapier integration, open your apps Authentication page in Zapier visual builder then select Session Auth in the drop-down. Before we go any further, we need to install the dependencies for our app: Right next to package.json should be index.js, which is the entry point to your app. Zapier includes your apps name in the connection label by default, followed by the version number, then any text you include in the connection label. For our app, we are going to set it up to include an API Key in a header. Then, test your authentication, adding a real account to ensure Zapier can successfully connect to your app and use your test API call. If this is your first time pushing your app version - we will ask you to provide a name so we can register your app - this is a one time thing! Also, recipe.js is just an example name of a model - maybe youll eventually make a contact.js, lead.js or order.js. Click Save & Continue when finished to save your authentication settings. For each field that you need, click the Add Fields button and fill in the details for your field. Zapier automatically creates a form for Basic Authall you need to add is a test API call to ensure those credentials work. # setup auth to Zapier's platform with a deploy key, # create a directory with the minimum required files, http://57b20fb546b57d1100a3c405.mockapi.io/api/recipes, // Edit the App definition to register our trigger, # 200 GET http://57b20fb546b57d1100a3c405.mockapi.io/api/recipes. Learn more in our Computed Fields docs. The API responds by sending an auth token to Zapier. Enter your 2FA authentication code from your authentication app when asked. Zapier automatically includes the API key and any additional input fields you added to your input form in the URL Params. Use an API endpoint that does not require any additional details or configuration, such as /me or /user to simply check the app authentication and retrieve details about the user. Optionally, click Switch to Code Mode to write custom JavaScript code for the API call instead of using the form inputs. Click the field to edit it, or click the gear icon and select Delete to remove a field. To begin building an app, use the init command to setup the needed structure. Zapier automatically adds a form where users will enter their username and password, so you dont need to configure anything for core basic auth. You cannot reorder fields, though you can delete fields and add them again if needed. The first thing we need to do is define the authentication section on the app. The CLI is the primary tool for managing your apps. Now your CLI is installed and ready to go! If you've lost your backup codes, but you still have your 2FA device, you can view your recovery codes by doing the following: Your 10 recovery codes will now be displayed in the Save Your Recovery Codes section. Zapier will automatically include the access token in subsequent API requests, but if you need to manually add it, the access token is stored in the authData bundle and can be referenced with {{bundle.authData.access_token}} or {{bundle.authData.accessToken}}, depending on how your APIs response references the access token. Zapier writes your deploy key to ~/.zapierrc. Zapier supports a wide range of authentication schemesselect the best one for your app. To generate new recovery codes: A new set of 10 recovery codes will be generated for you. The Access Token will be in the bundle.authData, and typically be referenced as {{bundle.authData.access_token}}, {{bundle.authData.sessionToken}}, or a similar field, depending on how your token exchange response includes the token. Zapier will automatically include the Client ID and Secret in authentication API calls, but if you need to reference them in your Zapier API calls or custom code, use the following codes: Now its time to configure how Zapier sends users to your API to allow access to their account in your app, and how Zapier gets the credentials it needs for future API calls. You can run your test again and make sure everything still works: So far, everything we have done has been local, on your machine. In Zapier integrations with OAuth v2, the user part of authentication typically takes place in full on the apps own site, helping users easily connect accounts without sharing account credentials or looking up API keys. Essentially, we export some metadata plus our listRecipes function and a sample. This will allow you to log into your Zapier account. Welcome to the Zapier Platform! Youll see the app listed as an available option for the first step. If you lose access to your Zapier account, our support team will not be able to restore access without your recovery codes. Tell us about your problem, and well find you a solution or you can email support. Open it up in your editor of choice and lets take a look! For input fields, select the default Field type, then add: Add the fields in the order users would expect to see them. # Copying project to temp directory - done! # Zipping project and dependencies - done! The first time you click the toggle, Zapier will convert the data from your API call form to JavaScript. You can have many versions of an app, which simplifies making breaking changes and testing in the future. If you have at least one trigger or action, now would be a fine time to share your app with some of your users. A classic way to do that with Zapier is to provide an input field a user can fill out. Click Connect an Account, and enter your GitHub username and password or personal access token. Click Save & Continue when finished to save your authentication settings. How to Use Dynamic Dropdowns in Zapier CLI, How to Add Logo and Branding to CLI Integration, How to Manage Versions of your Zapier Integration, How to Build and Publish a Zapier Integration, Zapier Integration Branding and Design Guidelines. If you need a specialized API call or response parsing on this or other API call steps, click Switch to Code Mode. Add an account for our app (enter any value you like for the API key, the mock API does not care). Zapier will pass the access token with the API call by default, as it will with all subsequent API calls, but if your API requires any additional configuration, click the Show Options button and add any options needed for a successful API call. # Installing project dependencies - done! Zapier then needs a Test API calltypically to a /user or /me endpoint that returns details about the user and needs no additional configurationto test account authentication and ensure the access token works. With that, weve successfully added authentication to our app! It's very important that you save your 10 recovery codes and put them in a safe place separate from your mobile device. Once youve added your input fields, Zapier lists each input field with its label, key, type, and required status on your authentication settings. If you havent already, in a different tab or window open your apps application, integration, or API settings, and add a new application for your OAuth integration with Zapier. Tell us about your problem and well connect you with the right resource or contact support. To do that, click the Switch to Code Mode toggle. Whenever a new account is authenticated with Zapier, Zapier adds a Connection Label so users can identify accounts and add multiple accounts if desired. for more details. Or run the command without an email address to get a URL you can put in your help pages or other messages to your users. If you need to change that, click the Show Options button and add any additional call details needed. In the dialog box, enter a verification code from your authentication app. Video courses designed to help you become a better Zapier user, whether youre a beginner or more experienced. You can typically copy this URL from your application or integration settings where you copied the client ID and secret previously, or from your apps API page. From Zapiers visual builder, copy the OAuth Redirect URL, similar to the one shown above, and add it to your applications integration settings. Then, test your authentication, adding a real account to ensure Zapier can successfully connect to your app and use your test API call. Zapier shows a popup window where users login and select their account with OAuth2, or where they can enter account details with basic auth. Lets push a version of your app! Then test the authentication. By default, Zapier will pass the client ID, client secret, authorization code, redirect URI, and a standard authorization_code grant type in the API request body. API Key authentication works similarly to Zapiers Basic Auth in that Zapier passes the credentials with every API call. Zapier will create a form with input fields to collect the username and password automatically. If your API supports automated token refresh, add your APIs Refresh Token Request in the following field, and check the Automatically Refresh Token box. Enter the same key name that your API uses. For different types of authentication, see these example apps: For this app, our API Key will go in the header. To make our helper function take effect, we need to register it on our app. For the test API call, enter an API endpoint under the Test header where Zapier can test users credentials for your app, and set the correct call method (typically GET). Check your API documentation for what fields are required, including user or account names, domains, and more. This is where the Platform will look to understand how your app will interact with Zapier. Its been fun, but we want our app on Zapier.com so we can use it with the thousands of other integrations! If you switch back to form mode, Zapier will save your custom code but will not use it in the API call. You can see from the detailed log that the request included our auth header. Every input field requires a Key, the name your API uses to reference this field. We will configure it to read data from a mocked API (in the future - your real app will use a real API, of course :-): Note: The triggers folder is simply a convention - we recommend it. Not within the Platform UI. Optionally, if you wish to limit Zapiers scope to let it only access specific data from your app, you can add OAuth scopes in the following field with a comma- or space-separated list. API Key authentication lets apps verify users account with an API key that is passed along with every API call. Changing your two-factor authentication device or app, re-enable it following these instructions. When to use API key authentication: Use API key authentication if your API primarily uses an API key to identify accounts, especially with apps for weather, maps, content verification, file conversion, and other data tools that require a key for access to the service but do not contain user-specific content. Inside the directory, youll see a few files. Only using a password to protect your account is still susceptible to security threats because it only represents a single piece of information someone needs to acquire. Click the Switch to Code Mode toggle to enable it. Tell us about your problem and well connect you with the right resource or contact support. Replace the example email address with the user you want to invite. Begin to log in to Zapier as you normally would. Check our Authentication Testing docs for more details, common errors you may encounter, and how to resolve those. All values to be saved should be returned at the top level, and will be referenced via {{bundle.authData.field}}, where field is the key in the response.
zapier authentication