@@ -6,7 +6,7 @@ See www.pipedrive.com for details.
66This is the official Pipedrive API wrapper-client for NodeJS based apps, distributed by Pipedrive Inc freely under the MIT licence.
77It provides convenient access to the Pipedrive API, allowing you to operate with objects such as Deals, Persons, Organizations, Products and much more.
88
9- ## Table of Contents
9+ ## Table of Contents
1010- [ Installation] ( #installation )
1111
1212- [ API Reference] ( #api-reference )
@@ -56,23 +56,32 @@ const pipedrive = require('pipedrive');
5656
5757const PORT = 1800 ;
5858
59- const defaultClient = new pipedrive.ApiClient ();
60-
61- // Configure API key authorization: apiToken
62- let apiToken = defaultClient .authentications .api_key ;
63- apiToken .apiKey = ' YOUR_API_TOKEN_HERE' ;
64-
6559app .listen (PORT , () => {
66- console .log (` Listening on port ${ PORT } ` );
60+ console .log (` Listening on port ${ PORT } ` );
6761});
6862
6963app .get (' /' , async (req , res ) => {
70- const api = new pipedrive.DealsApi (defaultClient);
64+ try {
65+ const apiClient = new pipedrive.ApiClient ();
66+
67+ // Configure API key authorization: apiToken
68+ let apiToken = apiClient .authentications .api_key ;
69+ apiToken .apiKey = ' YOUR_API_TOKEN_HERE' ;
70+
71+ const api = new pipedrive.DealsApi (apiClient);
7172 const deals = await api .getDeals ();
7273
73- res .send (deals);
74+ return res .send (deals);
75+ } catch (error) {
76+ console .error (' Error:' , error);
77+
78+ res .status (500 ).json ({
79+ error: error .message ,
80+ });
81+ }
7482});
7583
84+
7685```
7786
7887### With OAuth2
@@ -238,53 +247,63 @@ const cookieParser = require('cookie-parser');
238247const cookieSession = require (' cookie-session' );
239248
240249app .use (cookieParser ());
241- app .use (cookieSession ({
250+ app .use (
251+ cookieSession ({
242252 name: ' session' ,
243- keys: [' key1' ]
244- }));
253+ keys: [' key1' ],
254+ }),
255+ );
245256const PORT = 1800 ;
246257
247258const pipedrive = require (' pipedrive' );
248259
249- const apiClient = new pipedrive.ApiClient ();
250-
251- let oauth2 = apiClient .authentications .oauth2 ;
252- oauth2 .clientId = ' clientId' ; // OAuth 2 Client ID
253- oauth2 .clientSecret = ' clientSecret' ; // OAuth 2 Client Secret
254- oauth2 .redirectUri = ' http://localhost:1800/callback' ; // OAuth 2 Redirection endpoint or Callback Uri
255-
256260app .listen (PORT , () => {
257- console .log (` Listening on port ${ PORT } ` );
261+ console .log (` Listening on port ${ PORT } ` );
258262});
259263
260264app .get (' /' , async (req , res ) => {
261- if (req .session .accessToken !== null && req .session .accessToken !== undefined ) {
262- // token is already set in the session
263- // now make API calls as required
264- // client will automatically refresh the token when it expires and call the token update callback
265- const api = new pipedrive.DealsApi (apiClient);
266- const deals = await api .getDeals ();
267-
268- res .send (deals);
269- } else {
270- const authUrl = apiClient .buildAuthorizationUrl ();;
271-
272- res .redirect (authUrl);
273- }
265+ const apiClient = new pipedrive.ApiClient ();
266+
267+ let oauth2 = apiClient .authentications .oauth2 ;
268+ oauth2 .clientId = ' clientId' ; // OAuth 2 Client ID
269+ oauth2 .clientSecret = ' clientSecret' ; // OAuth 2 Client Secret
270+ oauth2 .redirectUri = ' http://localhost:1800/callback' ; // OAuth 2 Redirection endpoint or Callback Uri
271+
272+ if (
273+ req .session .accessToken !== null &&
274+ req .session .accessToken !== undefined
275+ ) {
276+ // token is already set in the session
277+ // now make API calls as required
278+ // client will automatically refresh the token when it expires and call the token update callback
279+ const api = new pipedrive.DealsApi (apiClient);
280+ const deals = await api .getDeals ();
281+
282+ res .send (deals);
283+ } else {
284+ const authUrl = apiClient .buildAuthorizationUrl ();
285+
286+ res .redirect (authUrl);
287+ }
274288});
275289
276290app .get (' /callback' , (req , res ) => {
277- const authCode = req .query .code ;
278- const promise = apiClient .authorize (authCode);
279-
280- promise .then (() => {
281- req .session .accessToken = apiClient .authentications .oauth2 .accessToken ;
282- res .redirect (' /' );
283- }, (exception ) => {
284- // error occurred, exception will be of type src/exceptions/OAuthProviderException
285- });
291+ const authCode = req .query .code ;
292+ const promise = apiClient .authorize (authCode);
293+
294+ promise .then (
295+ () => {
296+ req .session .accessToken = apiClient .authentications .oauth2 .accessToken ;
297+ res .redirect (' /' );
298+ },
299+ (exception ) => {
300+ // error occurred, exception will be of type src/exceptions/OAuthProviderException
301+ },
302+ );
286303});
287304
305+
306+
288307```
289308
290309## Documentation for Authorization
@@ -312,7 +331,7 @@ app.get('/callback', (req, res) => {
312331- ** Type** : OAuth
313332- ** Flow** : accessCode
314333- ** Authorization URL** : https://oauth.pipedrive.com/oauth/authorize
315- - ** Scopes** :
334+ - ** Scopes** :
316335 - base: Read settings of the authorized user and currencies in an account
317336 - deals:read: Read most of the data about deals and related entities - deal fields, products, followers, participants; all notes, files, filters, pipelines, stages, and statistics. Does not include access to activities (except the last and next activity related to a deal)
318337 - deals:full: Create, read, update and delete deals, its participants and followers; all files, notes, and filters. It also includes read access to deal fields, pipelines, stages, and statistics. Does not include access to activities (except the last and next activity related to a deal)
@@ -344,7 +363,7 @@ app.get('/callback', (req, res) => {
344363
345364All URIs are relative to * https://api.pipedrive.com/v1 *
346365
347- Code examples are available through the links in the list below or on the
366+ Code examples are available through the links in the list below or on the
348367[ Pipedrive Developers Tutorials] ( https://pipedrive.readme.io/docs/tutorials ) page
349368
350369Class | Method | HTTP request | Description
0 commit comments