Manual OAuth 2.0 for Zendesk


Get Started

Not the template you're looking for? Browse more.

About the template


This template demonstrates how to manually implement OAuth 2.0 flow for Zendesk. Get started to learn more.

About ScriptRunner Connect


What is ScriptRunner Connect?

ScriptRunner Connect is an AI assisted code-first (JavaScript/TypeScript) integration platform (iPaaS) for building complex integrations and automations.

Can I try it out for free?

Yes. ScriptRunner Connect comes with a forever free tier.

Can I customize the integration logic?

Absolutely. The main value proposition of ScriptRunner Connect is that you'll get full access to the code that is powering the integration, which means you can make any changes to the the integration logic yourself.

Can I change the integration to communicate with additional apps?

Yes. Since ScriptRunner Connect specializes in enabling complex integrations, you can easily change the integration logic to connect to as many additional apps as you need, no limitations.

What if I don't feel comfortable making changes to the code?

First you can try out our AI assistant which can help you understand what the code does, and also help you make changes to the code. Alternatively you can hire our professionals to make the changes you need or build new integrations from scratch.

Do I have to host it myself?

No. ScriptRunner Connect is a fully managed SaaS (Software-as-a-Service) product.

What about security?

ScriptRunner Connect is ISO 27001 and SOC 2 certified. Learn more about our security.

Template Content


README

Scripts

TypeScriptClearStorage
TypeScriptGetAccessToken
Async HTTP Event

README


๐Ÿ“‹ Overview

This template demonstrates how to manually implement OAuth 2.0 flow for Zendesk.

๐Ÿ–Š๏ธ Workspace setup

  • Create OAuth client app on Zendesk: https://support.zendesk.com/hc/en-us/articles/4408845965210#topic_s21_lfs_qk
  • Set up Async HTTP Event Listener and then fill out necessary parameters in Parameters, including the URL you received from the Event Listener.
  • Run CreateAuthLink script, copy authorization URL from console.
  • Open copied URL in a new browser tab and grant access to your OAuth client app.
  • Return to this Workspace, access token will be saved in Record Storage automatically.
  • (optional) In order to delete access token stored in Record Storage, run ClearStorage script. Keep in mind that you'll have to go through setup process all over again after that.

๐Ÿš€ Usage

Run MakeAPIRequest script to test the connection.

TypeScriptClearStorage

import { RecordStorage } from '@sr-connect/record-storage';

export default async function (event: any, context: Context): Promise<void> {
    const storage = new RecordStorage({ secure: true });

    await storage.deleteValue('access_token');
    
    console.log('All values deleted from Record Storage.');
}
TypeScriptGetAccessToken

import { HttpEventRequest } from '@sr-connect/generic-app/events/http';
import { RecordStorage } from '@sr-connect/record-storage';
import { EnvVars } from './Types';

export default async function (event: HttpEventRequest, context: Context): Promise<void> {
    if (context.triggerType === 'MANUAL') {
        console.error('This script is designed to be triggered externally or manually from the Event Listener. Please consider using Event Listener Test Event Payload if you need to trigger this script manually.');
        return;
    }

    const { CALLBACK_URL, CLIENT_ID, CLIENT_SECRET, INSTANCE_NAME, SCOPES } = context.environment.vars as EnvVars;

    const code = event.queryStringParams.code;

    // Check if code query param is included in response
    if (!code) {
        throw new Error('No code in event payload.');
    }

    const storage = new RecordStorage({ secure: true });

    // Request access token
    const response = await fetch(`https://${INSTANCE_NAME}.zendesk.com/oauth/tokens`, {
        method: 'POST',
        body: JSON.stringify({
            grant_type: 'authorization_code',
            code,
            client_id: CLIENT_ID,
            client_secret: CLIENT_SECRET,
            redirect_uri: CALLBACK_URL,
            scope: SCOPES.join(' ')
        }),
    });

    if (!response.ok) {
        throw new Error('Failed to get access token.');
    }

    const parsedResponse: { access_token: string } = await response.json();
    if (!parsedResponse.access_token) {
        throw new Error('No access token in response.');
    }

    // Save access token in Record Storage
    storage.setValue('access_token', parsedResponse.access_token);
    console.log('Access token saved in Record Storage.');
}
TypeScriptMakeAPIRequest

import { RecordStorage } from '@sr-connect/record-storage';
import { EnvVars } from './Types';

export default async function (event: any, context: Context): Promise<void> {
    const { INSTANCE_NAME } = context.environment.vars as EnvVars;

    const storage = new RecordStorage({ secure: true });
    const token = await storage.getValue('access_token');

    // Make request with Zendesk API
    const response = await fetch(`https://${INSTANCE_NAME}.zendesk.com/api/v2/tickets.json`, {
        headers: {
            'Authorization': `Bearer ${token}`
        }
    });

    if (!response.ok) {
        throw new Error('Failed to make API request.');
    }
    
    console.log(await response.json());
}
TypeScriptTypes

export interface EnvVars {
    readonly CALLBACK_URL: string;
    readonly CLIENT_ID: string;
    readonly CLIENT_SECRET: string;
    readonly INSTANCE_NAME: string;
    readonly SCOPES: string[];
}
Documentation ยท Support ยท Suggestions & feature requests