The official JavaScript package for the Patch API.
For a complete API reference, check out Patch's API Reference.
npm install @patch-technology/patch --save
yarn add @patch-technology/patch
- Node 10+
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+ import Patch from '@patch-technology/patch'; const patch = Patch('key_test_1234'); // ES5 var patch = require('@patch-technology/patch').default('key_test_1234');
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
// Create an order - you can create an order // providing either amount (and unit) or total_price (and currency), but not both // Create order with amount const amount = 1_000_000; // Pass in the amount in unit specified const unit = 'g'; patch.orders.createOrder({ amount: amount, unit: unit }); // Create an order with total price const totalPrice = 500; // Pass in the total price in smallest currency unit (ie cents for USD). const currency = 'USD'; patch.orders.createOrder({ total_price: totalPrice, currency: currency }); // Create order with the issued_to field (optional) const amount = 1_000_000; // Pass in the amount in unit specified const unit = 'g'; const issued_to = { email: 'issuee@companya.com', name: 'Olivia Jones' }; patch.orders.createOrder({ amount: amount, unit: unit, issued_to: issued_to }); // Retrieve an order orderId = 'ord_test_1234'; // Pass in the order's id patch.orders.retrieveOrder(orderId); // Place an order const orderId = 'ord_test_1234'; // Pass in the order's id patch.orders.placeOrder(orderId); // Place an order with the issued_to field (optional) const orderId = 'ord_test_1234'; // Pass in the order's id const issued_to = { email: 'issuee@companya.com', name: 'Olivia Jones' }; patch.orders.placeOrder(orderId, { issued_to: issued_to }); // Cancel an order const orderId = 'ord_test_1234'; // Pass in the order's id patch.orders.cancelOrder(orderId); // Retrieve a list of orders const page = 1; // Pass in which page of orders you'd like patch.orders.retrieveOrders({ page });
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:
countrytypeminimumAvailableMass
You can also set the acceptLanguage option to retrieve projects in a different language.
// Retrieve a project const projectId = 'pro_test_1234'; // Pass in the project's ID patch.projects.retrieveProject(projectId); // Retrieve a list of projects const page = 1; // Pass in which page of projects you'd like patch.projects.retrieveProjects({ page }); // Retrieve a filtered list of projects const country = 'CA'; // Pass in the country you'd like to get projects from patch.projects.retrieveProjects({ country }); // Retrieve a filtered list of projects const type = 'biomass'; // Pass in the project type you'd like to filter by patch.projects.retrieveProjects({ type }); // Retrieve a filtered list of projects const minimumAvailableMass = 100; // Pass in the minimum available inventory the projects should have patch.projects.retrieveProjects({ minimumAvailableMass }); // Retrieve a project in another language // See http://docs.patch.test:3000/#/internationalization for more information and support languages const projectId = 'pro_test_1234'; patch.projects.retrieveProject(projectId, { acceptLanguage: 'fr' });
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!
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
const Patch = require('@patch-technology/patch'); const patch = Patch.default(process.env.SANDBOX_API_KEY); patch.projects.retrieveProjects().then((response) => console.log(response));
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