@procore/js-sdk
TypeScript icon, indicating that this package has built-in type declarations

4.2.1 • Public • Published

Procore JS SDK

test npm version discord

A node.js JS SDK for the Procore API.

Note: ECMAScript target is ES5.

Requirements

  • Node.js version 14 or 16+, not 15.
    • We emphasize using the latest LTS version
  • A registered app on the Procore Developer Portal.
  • A Node.js web server (such as Express) for server authentication.

Installation

yarn add @procore/js-sdk

We recommend installing the package with yarn.

Making Requests

At the core of the package is the client object. Clients are initialized with a client_id and client_secret which can be obtained by signing up for Procore's Developer Program. The redirect_uri used for authorization must be specified on the Manage App page for the App associated with the client_id in use.

The Client object exposes #get, #post, #put, #patch, and #delete methods to you.

import * as sdk from '@procore/js-sdk';

const client = sdk.client(authorizer);

client.get(
  { base, version?, action?, params?, qs? }: EndpointConfig | string,
  {formatter?, companyId?, headers?}: RequestConfig
)

client.post(
  { base, version?, action?, params?, qs? }: EndpointConfig | string,
  payload,
  {formatter?, companyId?, headers?}: RequestConfig
)

client.put(
  { base, version?, action?, params?, qs? }: EndpointConfig | string,
  payload,
  {formatter?, companyId?, headers?}: RequestConfig
)

client.patch(
  { base, version?, action?, params?, qs? }: EndpointConfig | string,
  payload,
  {formatter?, companyId?, headers?}: RequestConfig
)

client.delete(
  { base, version?, action?, params?, qs? }: EndpointConfig | string,
  payload,
  {formatter?, companyId?, headers?}: RequestConfig
)

Request Config (RequestConfig)

RequestConfig supports 3 parameters:

  • formatter: Custom formatter function for response body.
  • companyId: Company Id used to set Procore-Company-Id header. Takes precedence over defaultCompanyId passed in ClientOptions.
  • headers: Custom headers passed as key/value pairs.
import * as sdk from '@procore/js-sdk';

const client = client(authorizer);

const customFormatter = async (response) => {
  if (response.body) {
    return await response.json();
  }
};

client.get(
  { base: "/projects" }, 
  {
    formatter: customFormatter,
    companyId: 1,
    headers: { "Acme-Customer-Header": "code" }
  }
);

Example

JS-SDK-Sample-App

Use js-sdk-sample-app as a getting started example application.

All paths are relative to https://{apiHostname}/{vapid|rest/{version}}/, the @procore/js-sdk will handle expanding them.

An API version may be specified in the version attribute to the client[method] function call, or the defaultVersion is used. The default version is v1.0 unless otherwise configured when instantiating the client (client(Authorizer, RequestInit, { defaultVersion: 'vapid' })).

A company id may be specified in the companyId attribute to the client[method] function call, or the defaultCompanyId value will be used when appending the Procore-Company-Id header to the request. (client(Authorizer, RequestInit, { defaultCompanyId: 10 })).

Example Requested URL
client.get({ base: '/example/{id}', params: { id: 42 } }) https://api.procore.com/rest/v1.0/example/42
client.get({ base: '/example/{id}', params: { id: 42 }, version: 'v1.1' }) https://api.procore.com/rest/v1.1/example/42
client.get({ base: '/example/{id}', params: { id: 42 }, version: 'vapid' }) https://api.procore.com/vapid/example/42

Responses

A single API response contains the response body (JSON parsed), original request, and complete response: { body, request, response }. isomorphic-fetch is the underlying http library, so both the request and response follow its specification. See fetch for more details.

import * as sdk from '@procore/js-sdk';

const client = sdk.client(authorizer);

client.get({
    base: '/projects',
    qs: { company_id: 1 }
  },
  {
    companyId: 1
  })
  .then({ body, request, response } => {
    console.log(body[0].name); // ACME Construction LLC.
    console.log(response.headers.get('Total')) // 865 (Total records for the resource)
  })
  .catch(error => {
    //Handle error
    console.log(error);
  });

or

import * as sdk from '@procore/js-sdk';

const client = sdk.client(authorizer);

async function getProjects() {
  const { body, request, response } = await client
    .get({
      base: '/projects',
      qs: { company_id: 1 }
    },
    {
      companyId: 1
    })
    .catch(error => {
    // Handle error
    console.log(error);
  });
  console.log(body[0].name); // ACME Construction LLC.
  console.log(response.headers.get('Total')) // 865 (Total records for the resource)
}
getProjects();

Formatting the response

By default, the SDK tries to format the body as JSON, you can control the formatting of the body by passing the formatter option as follows:

import * as sdk from '@procore/js-sdk';

const client = sdk.client(authorizer);
// Create your own formatter
function formatter(response) {
  // Your custom formatter code.
  // Response supports .text() and .json()
}

// Pass the formatter configuration
client.get({base: '/me'}, { formatter })

Multiple Procore Zones (MPZ)

All requests to the Procore API must include the Procore-Company-Id header to support Multiple Procore Zones (MPZ). See Multiple Procore Zones (MPZ) for more details. A Procore-Company-Id header will automatically be added to the request if the defaultCompanyId parameter is passed in the ClientOptions object or companyId parameter is passed in the RequestConfig object.

import * as sdk from '@procore/js-sdk';

// Pass the defaultCompanyId configuration in the ClientOptions
const client = sdk.client(authorizer, undefined, { defaultCompanyId: 10 });

client.get(
  { base: "/projects" }
);

or

import * as sdk from '@procore/js-sdk';

const client = sdk.client(authorizer);

// Pass the companyId configuration in the RequestConfig
client.get(
  { base: "/projects" },
  { companyId: procoreCompanyId }
);

Client Options (ClientOptions)

ClientOptions supports 3 parameters:

  • apiHostname: This is the hostname used for api requests. Default: https://api.procore.com
  • defaultVersion: Rest api version. Must in the format v\d.\d e.g. v1.0. Default: v1.0
  • defaultCompanyId: If companyId is not provided in RequestConfig this value will be used. Default: undefined
import * as sdk from '@procore/js-sdk';

const client = client(
  authorizer,
  undefined,
  { 
    apiHostname: "https://api.procore.com",
    defaultVersion: "v1.1",
    defaultCompanyId: 10,
  }
);

client.get({ base: "/projects" });

Tests

yarn && yarn test

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/procore/js-sdk. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

  1. Create PR with version change npm version minor
  2. Merge PR
  3. Circle Ci will release a new version of the package

Use npm publish --dry-run to verify the contents of your new version.

License

The package is available as open source under the terms of the MIT License.

About Procore

Procore Logo

The @procore/js-sdk is maintained by Procore Technologies.

Procore - building the software that builds the world.

Learn more about the #1 most widely used construction management software at procore.com

Readme

Keywords

none

Package Sidebar

Install

npm i @procore/js-sdk

Weekly Downloads

2,776

Version

4.2.1

License

MIT

Unpacked Size

49 kB

Total Files

32

Last publish

Collaborators

  • melch-procore
  • peterknif
  • moaz-ashraf
  • attachi
  • a.elbadawei
  • hyogman
  • dmitri_wm
  • stephanie.brereton
  • procore-oss-user
  • stevenkang3
  • max.helmetag
  • codyrobertsprocore
  • miguel.garcia-procore
  • magdyyx
  • atoaima
  • mustafa-abdelrahman
  • elewando-procore
  • ahmed.ghorab
  • lnspatz914
  • richard.bunn
  • omar.wagdy
  • mona.khairbek
  • mbartlett413
  • cody_schindler_procore
  • yoasyo25
  • ritchlee
  • andersontr15
  • steven.hinkle
  • jamie-dugan-procore
  • hgouhierprocore
  • denzylbalram
  • sarah.freitas
  • alan.bresani
  • amyprocore
  • yoyis3000
  • elijah.procore
  • mike-arndt-procore
  • jnhoang1
  • pam-whisenhunt
  • shradha.khard
  • david-christensen-procore
  • javio-procore
  • chance.eakin.procore
  • gideon-procore
  • ihor.diachenko_procore
  • justinmwatts
  • tedyang
  • jyang-procore
  • pwhisenhunt-procore
  • fairchild
  • rodayna.ehab
  • neil1023
  • scottstern
  • brian.smith1
  • g2mitchell
  • dlameter-procore
  • kylepietz
  • abhijit-procore
  • lhuang325
  • jake-pitkin
  • erikthoreson
  • simona.iancu
  • decha-sanson
  • aberkowitz
  • asamay
  • mustafa-u-abdelrahman
  • rajatmenhdiratta
  • jacksonleach-procore
  • pmfrawley
  • phunguyen-pcor
  • tatsiana.clifton
  • deiab
  • srichaitanya.peddinti
  • kenny.foisy
  • matheusprocore
  • jgreene_procore
  • hectorthiele
  • etokarev
  • daniel.ferreira-contractor
  • dmccraw-procore
  • cyrille.bai
  • greg.sparks
  • fabiomelo513
  • phil.custer
  • bbreyel921
  • amir-iskander
  • neil.mckeeman
  • nickprocore
  • lzhou888
  • davidshure
  • stevenliprocore
  • ramysaid2
  • refaiepcn
  • jgentes
  • faraz.hanif
  • mostafaeltazy
  • agamaleldin
  • andrew.isaac
  • saranahal2
  • rodrigo.dejuana
  • kellen.stewart
  • bill-wagner
  • ezrasimeloff
  • jeffgiaquinto
  • gturkadze
  • sean.spearman.procore
  • kylemartinez-procore
  • roobo-romeski
  • andres-mendez-procore
  • gaurav.sharma.procore
  • tracy.otto
  • sarah.heredia
  • victorbendeck-pc
  • cbathgate
  • davidkangpro
  • kyle.liu
  • amin.jaipuri
  • grafffffff
  • mishaelowoyemi
  • evan.cerwonka.procore
  • ilya.dryha-contractor
  • varomir
  • yogevfine1
  • timofeee
  • matt.harris0223
  • winson.chu
  • andersonbispoprocore
  • scorgiat-procore
  • ladavarga
  • procore_halzy
  • enyaga
  • willpankonien
  • sateesh-kadiyala-procore
  • chris.berber
  • txin1
  • epalinprocore
  • mehrdad-panahandeh
  • tyler.wasden.procore
  • jeremy.lund
  • dineshkumar.jayak
  • ryanfuentesprocore
  • stajics
  • brocktillotsonprocore
  • kyle.williams
  • dtorres-procore
  • noor.ali
  • ari-procore
  • alanprocore
  • jl4ever
  • james.lawson
  • ajaykumar-procore
  • dennis.heckman
  • tara.chambers
  • lalovar-procore
  • james.cleary
  • chadryder
  • devin.cunningham.procore
  • abhijit.patwardhan
  • lydiahara
  • sherylnapigkit
  • changprocore
  • apcarroll_procore
  • andy.mayer
  • bob.laskowski
  • vinaya-procore
  • kahliholmes
  • andrew.wheeler
  • leandro-proc
  • yadhu.prakash
  • jason-kaye
  • jesse.olsen
  • patrick.lardin
  • brad.urani
  • allenanle.procore
  • brookyboy009
  • uddhavjoglekar
  • dancingshell
  • rysmithprocore
  • robbiegprocore
  • jadamsss
  • jeremy.bouzigard
  • timdoherty
  • b.bookout
  • jalyng
  • htael
  • dev-account-admin
  • sseanwang
  • bhargavrnd
  • farismmk
  • dannyporrello
  • danny.ou
  • messanjah
  • eyvettesou
  • jgee67
  • cagmz
  • mariah_delaney
  • lukenispel
  • kimhin267
  • juliana.hernandez
  • judy-lu-pc
  • procore-it-support
  • andrewburke-pc
  • jkleintech
  • rachel.arkebauer
  • procore-npm-bot
  • james.dabbs-procore
  • laurenbrandsteinprocore
  • scottbieser-procore
  • zach.mckenzie.procore
  • shayonj_procore
  • heplayskeys
  • mike.south
  • thomasoboyle
  • dischorde
  • derek-carter-procore
  • dlgasser
  • cfprocore
  • evan.waits
  • jeremy-marcus
  • jmejia-fsl
  • ersgonzalo
  • stephan-procore
  • aleclarsenprocore
  • yihai.zweifel
  • jay-rajan
  • jacky-lei
  • peter.jin