|
| 1 | +# Patch Python SDK |
| 2 | + |
| 3 | +[](https://badge.fury.io/rb/patch_ruby) |
| 4 | +[](https://discord.gg/AU8543D) |
| 5 | + |
| 6 | +The official Python library for the [Patch API](https://www.usepatch.com). |
| 7 | + |
| 8 | +## Documentation |
| 9 | +For a complete API reference, check out [Patch's API Reference.](https://docs.usepatch.com/). |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +Add the library to your `requirements.txt` file: |
| 14 | +```txt |
| 15 | +patch >= 1.0.0 |
| 16 | +``` |
| 17 | + |
| 18 | +Then run: |
| 19 | +```shell |
| 20 | +pip install |
| 21 | +``` |
| 22 | + |
| 23 | +Or install it directly with |
| 24 | +```shell |
| 25 | +pip install patch |
| 26 | +``` |
| 27 | + |
| 28 | +### Requirements |
| 29 | +- Python 3.0.0 |
| 30 | + |
| 31 | +## Usage |
| 32 | + |
| 33 | +### Configuration |
| 34 | + |
| 35 | +After installing the gem, you'll have to configure it with your API key which is available from the API key page in the Patch dashboard: |
| 36 | + |
| 37 | +```python |
| 38 | +import patch_api |
| 39 | + |
| 40 | +configuration = patch_api.Configuration(api_key=os.environ.get('SANDBOX_API_KEY')) |
| 41 | +api_client = patch_api.ApiClient(configuration) |
| 42 | +``` |
| 43 | + |
| 44 | +The `api_client` will be used to instantiate other API objects for Patch resources, for example the `OrdersApi`: |
| 45 | + |
| 46 | +``` |
| 47 | +import patch_api |
| 48 | +from patch_api.api.orders_api import OrdersApi |
| 49 | +
|
| 50 | +configuration = patch_api.Configuration(api_key=os.environ.get('SANDBOX_API_KEY')) |
| 51 | +api_client = patch_api.ApiClient(configuration) |
| 52 | +orders_api = OrdersApi(api_client=api_client) |
| 53 | +``` |
| 54 | + |
| 55 | + |
| 56 | +### Orders |
| 57 | +In Patch, orders represent a purchase of carbon offsets or negative emissions by mass. Place orders directly if you know the amount of carbon dioxide you would like to sequester. If you do not know how much to purchase, use an estimate. |
| 58 | + |
| 59 | +In Patch, orders represent a purchase of carbon offsets or negative emissions by mass. |
| 60 | +Place orders directly if you know the amount of carbon dioxide you would like to sequester. |
| 61 | +If you do not know how much to purchase, use an estimate. |
| 62 | +You can also create an order with a maximum desired price, and we'll allocate enough mass to |
| 63 | +fulfill the order for you. |
| 64 | + |
| 65 | +[API Reference](https://docs.usepatch.com/#/?id=orders) |
| 66 | + |
| 67 | +#### Examples |
| 68 | +```python |
| 69 | +# Create an order - you can create an order |
| 70 | +# providing either mass_g or total_price_cents_usd, but not both |
| 71 | + |
| 72 | +from patch_api.models.create_order_request import CreateOrderRequest |
| 73 | + |
| 74 | +# Create order with mass |
| 75 | +mass_g = 1_000_000 # Pass in the mass in grams (i.e. 1 metric tonne) |
| 76 | +create_order_request = CreateOrderRequest(mass_g=mass_g) |
| 77 | +OrdersApi.create_order(create_order_request) |
| 78 | + |
| 79 | +# Create an order with maximum total price |
| 80 | +total_price_cents_usd = 5_00 # Pass in the total price in cents (i.e. 5 dollars) |
| 81 | +create_order_request = CreateOrderRequest(total_price_cents_usd=total_price_cents_usd) |
| 82 | +OrdersApi.create_order(create_order_request) |
| 83 | + |
| 84 | +## You can also specify a project-id field (optional) to be used instead of the preferred one |
| 85 | +project_id = 'pro_test_1234' # Pass in the project's ID |
| 86 | +create_order_request = CreateOrderRequest(project_id=project_id, mass_g=mass_g) |
| 87 | +OrdersApi.create_order(create_order_request) |
| 88 | + |
| 89 | + |
| 90 | +## Orders also accept a metadata field (optional) |
| 91 | +metadata = {user: "john doe"} |
| 92 | +create_order_request = CreateOrderRequest(metadata=metadata, mass_g=mass_g) |
| 93 | +OrdersApi.create_order(create_order_request) |
| 94 | + |
| 95 | + |
| 96 | +# Retrieve an order |
| 97 | +order_id = 'ord_test_1234' # Pass in the order's id |
| 98 | +OrdersApi.retrieve_order(id=order_id) |
| 99 | + |
| 100 | +# Place an order |
| 101 | +order_id = 'ord_test_1234' # Pass in the order's id |
| 102 | +OrdersApi.place_order(id=order_id) |
| 103 | + |
| 104 | +# Cancel an order |
| 105 | +order_id = 'ord_test_1234' # Pass in the order's id |
| 106 | +OrdersApi.cancel_order(id=order_id) |
| 107 | + |
| 108 | +# Retrieve a list of orders |
| 109 | +page = 1 # Pass in which page of orders you'd like |
| 110 | +OrdersApi.retrieve_orders(page=page) |
| 111 | +``` |
| 112 | + |
| 113 | +### Estimates |
| 114 | +Estimates allow API users to get a quote for the cost of compensating a certain amount of CO2. When creating an estimate, an order in the `draft` state will also be created, reserving the allocation of a project for 5 minutes. If you don't place your draft order within those 5 minutes, the order will automatically be cancelled. |
| 115 | + |
| 116 | +[API Reference](https://docs.usepatch.com/#/?id=estimates) |
| 117 | + |
| 118 | +#### Examples |
| 119 | +```python |
| 120 | +# Create an estimate |
| 121 | +from patch_api.models.create_estimate_request import CreateEstimateRequest |
| 122 | + |
| 123 | +mass_g = 1_000_000 # Pass in the mass in grams (i.e. 1 metric tonne) |
| 124 | +create_estimate_request = CreateEstimateRequest(mass_g=mass_g) |
| 125 | +EstimatesApi.create_estimate(create_estimate_request) |
| 126 | + |
| 127 | +## You can also specify a project-id field (optional) to be used instead of the preferred one |
| 128 | +project_id = 'pro_test_1234' # Pass in the project's ID |
| 129 | +create_estimate_request = CreateEstimateRequest(mass_g=mass_g, project_id=project_id) |
| 130 | +EstimatesApi.create_estimate(create_estimate_request) |
| 131 | + |
| 132 | +# Retrieve an estimate |
| 133 | +estimate_id = 'est_test_1234' |
| 134 | +EstimatesApi.retrieve_estimate(id=estimate_id) |
| 135 | + |
| 136 | +# Retrieve a list of estimates |
| 137 | +page = 1 # Pass in which page of estimates you'd like |
| 138 | +EstimatesApi.retrieve_estimates(page=page) |
| 139 | +``` |
| 140 | + |
| 141 | +### Projects |
| 142 | +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. |
| 143 | + |
| 144 | +[API Reference](https://docs.usepatch.com/#/?id=projects) |
| 145 | + |
| 146 | +#### Examples |
| 147 | +```python |
| 148 | +# Retrieve a project |
| 149 | +project_id = 'pro_test_1234' # Pass in the project's ID |
| 150 | +ProjectsApi.retrieve_project(id=project_id) |
| 151 | + |
| 152 | +# Retrieve a list of projects |
| 153 | +page = 1 # Pass in which page of projects you'd like |
| 154 | +ProjectsApi.retrieve_projects(page=page) |
| 155 | +``` |
| 156 | + |
| 157 | +### Preferences |
| 158 | +Preferences are how you route your orders in Patch. If you don't have a preference, Patch will allocate your order to the least expensive option. If you do have a preference, all of your orders will be sent to that project. You can set your preferences via API, or through the [Patch Dashboard](https://dashboard.usepatch.com/projects). |
| 159 | + |
| 160 | +[API Reference](https://docs.usepatch.com/#/?id=preferences) |
| 161 | + |
| 162 | +#### Examples |
| 163 | +```python |
| 164 | +# Create a preference |
| 165 | +from patch_api.models.create_preference_request import CreatePreferenceRequest |
| 166 | + |
| 167 | +project_id = 'pro_test_1234' # Pass in the project_id for your preference |
| 168 | +create_preference_request = CreatePreferenceRequest(project_id=project_id) |
| 169 | +PreferencesApi.create_preference(create_preference_request) |
| 170 | + |
| 171 | +# Retrieve a preference |
| 172 | +preference_id = 'pre_test_1234' # Pass in the preferences's id |
| 173 | +PreferencesApi.retrieve_preference(preference_id=preference_id) |
| 174 | + |
| 175 | +# Delete a preference |
| 176 | +preference_id = 'pre_test_1234' # Pass in the preferences's id |
| 177 | +PreferencesApi.delete_preference(preference_id=preference_id) |
| 178 | + |
| 179 | +# Retrieve a list of preferences |
| 180 | +page = 1 # Pass in which page of preferences you'd like |
| 181 | +PreferencesApi.retrieve_preferences(page=page) |
| 182 | +``` |
| 183 | + |
| 184 | +## Development |
| 185 | + |
| 186 | +To build the library locally, run: |
| 187 | +``` |
| 188 | +$ make build |
| 189 | +``` |
| 190 | + |
| 191 | +To test the library locally, create a python file in a sibling directory and add the following: |
| 192 | +```python |
| 193 | +import sys |
| 194 | +sys.path.append("../patch-python") |
| 195 | + |
| 196 | +import patch_api |
| 197 | + |
| 198 | +# ..... your Patch API code goes here |
| 199 | +``` |
| 200 | + |
| 201 | +Set up required environment variables: |
| 202 | +``` |
| 203 | +$ export SANDBOX_API_KEY=<SANDBOX API KEY> |
| 204 | +``` |
| 205 | + |
| 206 | +Run tests: |
| 207 | +``` |
| 208 | +$ make test |
| 209 | +``` |
0 commit comments