Node.js SDK for Zoho CRM API v2.0 providing OAuth authentication, multi-user support, and complete CRUD operations for CRM data
The Zoho CRM SDK implements OAuth 2.0 authentication with flexible token storage options, supporting file-based storage, database storage, and custom storage implementations.
Create OAuth tokens for authentication with various configuration options.
/**
* Builder for creating OAuth tokens with different authentication flows
*/
class OAuthBuilder {
/** Set OAuth client ID */
clientId(clientId: string): OAuthBuilder;
/** Set OAuth client secret */
clientSecret(clientSecret: string): OAuthBuilder;
/** Set refresh token for token refresh flow */
refreshToken(refreshToken: string): OAuthBuilder;
/** Set grant token for initial authorization */
grantToken(grantToken: string): OAuthBuilder;
/** Set access token for direct token usage */
accessToken(accessToken: string): OAuthBuilder;
/** Set OAuth redirect URL */
redirectURL(redirectURL: string): OAuthBuilder;
/** Set unique identifier for token persistence */
id(id: string): OAuthBuilder;
/** Build the OAuth token */
build(): OAuthToken;
}Usage Examples:
const { OAuthBuilder } = require("@zohocrm/nodejs-sdk-2.0/models/authenticator/oauth_builder");
// Using refresh token (recommended for production)
const token = new OAuthBuilder()
.clientId("your_client_id")
.clientSecret("your_client_secret")
.refreshToken("your_refresh_token")
.redirectURL("https://your-app.com/callback")
.build();
// Using grant token (for initial setup)
const grantToken = new OAuthBuilder()
.clientId("your_client_id")
.clientSecret("your_client_secret")
.grantToken("your_grant_token")
.redirectURL("https://your-app.com/callback")
.build();
// Using access token directly
const accessToken = new OAuthBuilder()
.accessToken("your_access_token")
.build();
// Using stored token ID
const storedToken = new OAuthBuilder()
.id("unique_token_id")
.build();The OAuth token implementation with all authentication details.
/**
* OAuth token containing authentication information
*/
class OAuthToken {
/** Get client ID */
getClientId(): string;
/** Get client secret */
getClientSecret(): string;
/** Get access token */
getAccessToken(): string;
/** Get refresh token */
getRefreshToken(): string;
/** Get redirect URL */
getRedirectURL(): string;
/** Get grant token */
getGrantToken(): string;
/** Get token ID */
getId(): string;
/** Get token expiry time */
getExpiryTime(): string;
/** Set access token */
setAccessToken(accessToken: string): void;
/** Set refresh token */
setRefreshToken(refreshToken: string): void;
/** Set expiry time */
setExpiryTime(expiryTime: string): void;
}Abstract base class for implementing token persistence strategies.
/**
* Abstract token store for persisting OAuth tokens
*/
abstract class TokenStore {
/**
* Retrieve a token for a specific user
* @param user - The user signature
* @param token - Token instance for type matching
* @returns The stored token or null if not found
*/
getToken(user: UserSignature, token: Token): Promise<Token>;
/**
* Save a token for a specific user
* @param user - The user signature
* @param token - The token to save
*/
saveToken(user: UserSignature, token: Token): Promise<void>;
/**
* Delete a specific token
* @param token - The token to delete
*/
deleteToken(token: Token): Promise<void>;
/**
* Get all stored tokens
* @returns Array of all tokens
*/
getTokens(): Promise<Token[]>;
/**
* Delete all stored tokens
*/
deleteTokens(): Promise<void>;
/**
* Get token by unique identifier
* @param id - Unique token identifier
* @param token - Token instance for type matching
* @returns The token if found
*/
getTokenById(id: string, token: Token): Promise<Token>;
}Store OAuth tokens in a local file.
/**
* File-based token storage implementation
*/
class FileStore extends TokenStore {
/**
* Create file store with specified file path
* @param filePath - Absolute path to token storage file
*/
constructor(filePath: string);
/** Get token from file storage */
getToken(user: UserSignature, token: Token): Promise<Token>;
/** Save token to file storage */
saveToken(user: UserSignature, token: Token): Promise<void>;
/** Delete token from file storage */
deleteToken(token: Token): Promise<void>;
/** Get all tokens from file storage */
getTokens(): Promise<Token[]>;
/** Delete all tokens from file storage */
deleteTokens(): Promise<void>;
/** Get token by ID from file storage */
getTokenById(id: string, token: Token): Promise<Token>;
}File Storage Usage Example:
const { FileStore } = require("@zohocrm/nodejs-sdk-2.0/models/authenticator/store/file_store");
// Create file store
const tokenStore = new FileStore("/Users/username/Documents/zoho_tokens.txt");
// Use with initialization
await new InitializeBuilder()
.user(user)
.environment(environment)
.token(token)
.store(tokenStore)
.initialize();Store OAuth tokens in a MySQL database with configurable connection settings.
/**
* Database builder for MySQL token storage
*/
class DBBuilder {
/** Set database host (default: localhost) */
host(host: string): DBBuilder;
/** Set database name (default: zohooauth) */
databaseName(databaseName: string): DBBuilder;
/** Set database username (default: root) */
userName(userName: string): DBBuilder;
/** Set database password (default: empty) */
password(password: string): DBBuilder;
/** Set database port (default: 3306) */
portNumber(portNumber: string): DBBuilder;
/** Set table name (default: oauthtoken) */
tableName(tableName: string): DBBuilder;
/** Build the database store */
build(): DBStore;
}
/**
* Database token storage implementation
*/
class DBStore extends TokenStore {
/** Get token from database */
getToken(user: UserSignature, token: Token): Promise<Token>;
/** Save token to database */
saveToken(user: UserSignature, token: Token): Promise<void>;
/** Delete token from database */
deleteToken(token: Token): Promise<void>;
/** Get all tokens from database */
getTokens(): Promise<Token[]>;
/** Delete all tokens from database */
deleteTokens(): Promise<void>;
/** Get token by ID from database */
getTokenById(id: string, token: Token): Promise<Token>;
}Database Storage Usage Example:
const { DBBuilder } = require("@zohocrm/nodejs-sdk-2.0/models/authenticator/store/db_builder");
// Configure database connection
const tokenStore = new DBBuilder()
.host("localhost")
.databaseName("zohooauth")
.userName("dbuser")
.password("dbpassword")
.portNumber("3306")
.tableName("oauth_tokens")
.build();
// Use with initialization
await new InitializeBuilder()
.user(user)
.environment(environment)
.token(token)
.store(tokenStore)
.initialize();Required Database Schema:
CREATE TABLE oauthtoken (
id varchar(255) NOT NULL,
user_mail varchar(255) NOT NULL,
client_id varchar(255),
client_secret varchar(255),
refresh_token varchar(255),
access_token varchar(255),
grant_token varchar(255),
expiry_time varchar(20),
redirect_url varchar(255),
primary key (id)
);Implement custom token storage by extending the TokenStore class.
/**
* Custom token store implementation example
*/
class CustomStore extends TokenStore {
constructor() {
super();
}
/**
* Implement custom token retrieval logic
* @param user - User signature
* @param token - Token instance for type matching
* @returns Stored token or null
*/
getToken(user: UserSignature, token: Token): Promise<Token> {
// Custom implementation
return null;
}
/**
* Implement custom token saving logic
* @param user - User signature
* @param token - Token to save
*/
saveToken(user: UserSignature, token: Token): Promise<void> {
// Custom implementation
}
/**
* Implement custom token deletion logic
* @param token - Token to delete
*/
deleteToken(token: Token): Promise<void> {
// Custom implementation
}
/**
* Implement custom token retrieval for all tokens
* @returns Array of all stored tokens
*/
getTokens(): Promise<Token[]> {
// Custom implementation
return [];
}
/**
* Implement custom deletion of all tokens
*/
deleteTokens(): Promise<void> {
// Custom implementation
}
/**
* Implement custom token retrieval by ID
* @param id - Unique token identifier
* @param token - Token instance for type matching
* @returns Token if found
*/
getTokenById(id: string, token: Token): Promise<Token> {
// Custom implementation
return null;
}
}Custom Storage Usage Example:
// Implement custom storage (e.g., Redis, MongoDB, etc.)
class RedisTokenStore extends TokenStore {
constructor(redisClient) {
super();
this.redis = redisClient;
}
async getToken(user, token) {
const key = `zoho_token:${user.getEmail()}`;
const tokenData = await this.redis.get(key);
return tokenData ? JSON.parse(tokenData) : null;
}
async saveToken(user, token) {
const key = `zoho_token:${user.getEmail()}`;
await this.redis.set(key, JSON.stringify(token));
}
async deleteToken(token) {
// Implementation for specific token deletion
}
// ... implement other required methods
}
// Use custom store
const customStore = new RedisTokenStore(redisClient);
await new InitializeBuilder()
.user(user)
.environment(environment)
.token(token)
.store(customStore)
.initialize();The base token interface that all token implementations must follow.
/**
* Base token interface
*/
interface Token {
/** Generate authentication header for API requests */
authenticate(): Promise<void>;
/** Remove authentication token */
remove(): Promise<boolean>;
}Install with Tessl CLI
npx tessl i tessl/npm-zohocrm--nodejs-sdk-2-0