Skip to content

Repository files navigation

Patch JavaScript SDK

Testnpm versionDiscord

The official JavaScript package for the Patch API.

Documentation

For a complete API reference, check out Patch's API Reference.

Installation

NPM

npm install @patch-technology/patch --save

Yarn

yarn add @patch-technology/patch

Requirements

  • Node 10+

Usage

Configuration

After installing the package, you'll have to configure it with your API key which is available from the API key page in the Patch dashboard:

// ES6+importPatchfrom'@patch-technology/patch';constpatch=Patch('key_test_1234');// ES5varpatch=require('@patch-technology/patch').default('key_test_1234');

Peer dependencies

For environments that do not include the Node Standard Library, such as React Native, you will need to install the listed peer dependencies in order for the package to work as expected. You can install the peer dependencies by running:

npm install install-peers

Orders

API Reference

Examples

// Create an order - you can create an order// providing either amount (and unit) or total_price (and currency), but not both// Create order with amountconstamount=1_000_000;// Pass in the amount in unit specifiedconstunit='g';patch.orders.createOrder({amount: amount,unit: unit});// Create an order with total priceconsttotalPrice=500;// Pass in the total price in smallest currency unit (ie cents for USD).constcurrency='USD';patch.orders.createOrder({total_price: totalPrice,currency: currency});// Create order with the issued_to field (optional)constamount=1_000_000;// Pass in the amount in unit specifiedconstunit='g';constissued_to={email: 'issuee@companya.com',name: 'Olivia Jones'};patch.orders.createOrder({amount: amount,unit: unit,issued_to: issued_to});// Retrieve an orderorderId='ord_test_1234';// Pass in the order's idpatch.orders.retrieveOrder(orderId);// Place an orderconstorderId='ord_test_1234';// Pass in the order's idpatch.orders.placeOrder(orderId);// Place an order with the issued_to field (optional)constorderId='ord_test_1234';// Pass in the order's idconstissued_to={email: 'issuee@companya.com',name: 'Olivia Jones'};patch.orders.placeOrder(orderId,{issued_to: issued_to});// Cancel an orderconstorderId='ord_test_1234';// Pass in the order's idpatch.orders.cancelOrder(orderId);// Retrieve a list of ordersconstpage=1;// Pass in which page of orders you'd likepatch.orders.retrieveOrders({ page });

Projects

Projects are the ways Patch takes CO2 out of the air. They can represent reforestation, enhanced weathering, direct air carbon capture, etc. When you place an order via Patch, it is allocated to a project.

When fetching Projects, you can add filters to the query to narrow the result. Currently supported filters are:

  • country
  • type
  • minimumAvailableMass

You can also set the acceptLanguage option to retrieve projects in a different language.

API Reference

Examples

// Retrieve a projectconstprojectId='pro_test_1234';// Pass in the project's IDpatch.projects.retrieveProject(projectId);// Retrieve a list of projectsconstpage=1;// Pass in which page of projects you'd likepatch.projects.retrieveProjects({ page });// Retrieve a filtered list of projectsconstcountry='CA';// Pass in the country you'd like to get projects frompatch.projects.retrieveProjects({ country });// Retrieve a filtered list of projectsconsttype='biomass';// Pass in the project type you'd like to filter bypatch.projects.retrieveProjects({ type });// Retrieve a filtered list of projectsconstminimumAvailableMass=100;// Pass in the minimum available inventory the projects should havepatch.projects.retrieveProjects({ minimumAvailableMass });// Retrieve a project in another language// See http://docs.patch.test:3000/#/internationalization for more information and support languagesconstprojectId='pro_test_1234';patch.projects.retrieveProject(projectId,{acceptLanguage: 'fr'});

Contributing

While we value open-source contributions to this SDK, the core of this library is generated programmatically. Complex additions made directly to the library would have to be moved over to our generation code, otherwise they would be overwritten upon the next generated release. Feel free to open a PR as a proof of concept, but know that we will not be able to merge it as-is. We suggest opening an issue first to discuss with us!

On the other hand, contributions to the README, as well as new test cases are always very welcome!

Build and manually test

To build and test the package locally, run:

$ npm run build

This will generate a dist folder with the compiled code. Next you want to link the package and use it in a different folder.

In the patch-node folder, run:

$ npm link

Navigate to a different, empty folder:

$ cd ..
$ mkdir test-patch-node
$ cd test-patch-node

In that repository, run the following command to use the locally built package:

$ npm link @patch-technology/patch

This will create a node_modules directory in your test repository which will symlink to your locally built package. To test out the package, open a node REPL and import the package and run some queries.

SANDBOX_API_KEY=xxx node
constPatch=require('@patch-technology/patch');constpatch=Patch.default(process.env.SANDBOX_API_KEY);patch.projects.retrieveProjects().then((response)=>console.log(response));

Run the specs

Before running the tests, make sure you set the test API key! Please use test API keys and not production ones, they usually start with key_test_. Be sure you navigate back to the root patch-node directory to run the tests.

$ export SANDBOX_API_KEY=<PATCH_TEST_API_KEY>

Then you are ready to run the tests:

$ npm run test

Releases

Used by

Contributors

Languages