Metadata
This page describes the APIs Onshape provides for working with metadata.
Prerequisites
|
---|
Updating Metadata
To update metadata, you send a JSON in the API request body in the following general format:
{
"properties": [
{
"value": "", // new value for the property
"propertyId": "{propertyId}" // ID of the property
}
]
}
Examples
Update a part property
Update a part’s description by getting the current metadata for the part and then posting an update to that metadata.
Endpoint
POST v10/metadata/d/{did}/w/{wid}/e/{eid}/p/{pid}
Request Body
{
"jsonType": "metadata-part",
"partId": "{pid}",
"properties": [
{
"value": "",
"propertyId": "{propertyId}"
}
]
}
Additional optional fields available. See the API Explorer for a full list.
Example
- Make a copy of this public document. Make a note of the new document’s document ID, workspace ID, and element ID.
- Get the ID of the part to update. Call the getPartsWMVE endpoint on your document to get a list of part IDs in the element. Only one part exists in the document, with a part ID of
JHD
.{ "name": "Main", // ... "microversionId": "{mid}", "partNumber": null, "elementId": "{eid}", "partId": "JHD", "bodyType": "sheet", // ... }
- Now, get the metadata of the part by calling the getWMVEPMetadata endpoint to get the current metadata JSON for the part.
curl -X 'GET' \ 'https://{baseUrl}.onshape.com/api/v10/metadata/d/{did}/w/{wid}/e/{eid}/p/JHD' \ -H 'accept: application/json;charset=UTF-8; qs=0.09' \ -H 'Authorization: Basic CREDENTIALS' \ -H 'Content-Type: application/json;charset=UTF-8; qs=0.09'
import requests import json auth = (access_key, secret_key) # See Authentication guide headers = { 'Accept': 'application/json;charset=UTF-8;qs=0.09', 'Content-Type': 'application/json;charset=UTF-8; qs=0.09' } api_url = "https://{baseUrl}.onshape.com/api/v10/metadata/d/{did}/w/{wid}/e/{eid}/p/JHD" queryParams = {} response = requests.get( api_url, params=queryParams, auth=auth, headers=headers ) print(json.dumps(response.json(), indent=4))
- Locate the property to update in the response. Scroll to the
Description
properties block of the JSON response, and notice that thevalue
field is an empty string.{ "name": "Description", "value": "", "defaultValue": null, "computedPropertyError": null, "propertySource": 3, "validator": { }, "required": false, "editable": true, "propertyId": "{propertyId}", "editableInUi": true, "dateFormat": null, "valueType": "STRING", "enumValues": null, "multivalued": false, "computedAssemblyProperty": false, "computedProperty": false, "propertyOverrideStatus": 0 }
- Find the metadata’s property ID; in this example, we want the
propertyId
for theDescription
property. We’ll need this ID to update the metadata. - Make the updateWVEPMetadata call. Replace the
{propertyId}
value with the ID from the last step.curl -X 'POST' \ 'https://cad.onshape.com/api/v10/metadata/d/{did}/w/{wid}/e/{eid}/p/JHD' \ -H 'accept: application/json;charset=UTF-8; qs=0.09' \ -H 'Authorization: Basic CREDENTIALS' \ -H 'Content-Type: application/json;charset=UTF-8; qs=0.09' \ -d '{ "jsonType": "metadata-part", "partId": "JHD", "properties": [ { "value": "Drill bit", "propertyId": "{propertyId}" } ] }'
import requests import json auth = (access_key, secret_key) # See Authentication guide headers = { 'Accept': 'application/json;charset=UTF-8;qs=0.09', 'Content-Type': 'application/json;charset=UTF-8; qs=0.09' } api_url = "https://{baseUrl}.onshape.com/api/v10/metadata/d/{did}/w/{wid}/e/{eid}/p/JHD" queryParams = {} body = { "jsonType": "metadata-part", "partId": "JHD", "properties": [ { "value": "Drill bit", "propertyId": "{propertyId}" } ] } response = requests.post( api_url, params=queryParams, json=body, auth=auth, headers=headers ) print(json.dumps(response.json(), indent=4))
- Repeat steps 3 and 4 to confirm that the Description
value
for the part is nowDrill bit
.
Update a part number
Endpoint
POST v10/metadata/d/{did}/w/{wid}/e/{eid}/p/{pid}
Request Body
{
"jsonType": "metadata-part",
"partId": "{pid}",
"properties": [
{
"value": "newPartNumber",
"propertyId": "{propertyId}"
}
]
}
Additional optional fields available. See the API Explorer for a full list.
Example
- Get the next available part number for your part. See Release Management for instructions.
- Call getPartsWMVE on your document to get the
partId
to update. Use this as thepid
param in the next step. - Call getWMVEPMetadata to get the current metadata JSON for the part.
curl -X 'GET' \ 'https://{baseUrl}.onshape.com/api/v10/metadata/d/{did}/w/{wid}/e/{eid}/p/{pid}' \ -H 'accept: application/json;charset=UTF-8; qs=0.09' \ -H 'Authorization: Basic CREDENTIALS' \ -H 'Content-Type: application/json;charset=UTF-8; qs=0.09'
import requests import json auth = (access_key, secret_key) # See Authentication guide headers = { 'Accept': 'application/json;charset=UTF-8;qs=0.09', 'Content-Type': 'application/json;charset=UTF-8; qs=0.09' } api_url = "https://{baseUrl}.onshape.com/api/v10/metadata/d/{did}/w/{wid}/e/{eid}/p/{pid}" queryParams = {} response = requests.get( api_url, params=queryParams, auth=auth, headers=headers ) print(json.dumps(response.json(), indent=4))
- In the response, scroll to the
Part number
property object. Note thepropertyId
to use in the next step. Also note thevalue
– this is the current part number that we will update.
{
"properties": [
{
"name": "Part number",
"value": "PRT-001",
"defaultValue": null,
"computedPropertyError": null,
"computedPropertyEvalInfo": null,
"aggregationSkippedFilteredOutValues": false,
"required": false,
"validator": {},
"propertySource": 3,
"editable": true,
"valueType": "STRING",
"propertyId": "{propertyId}",
"editableInUi": true,
"dateFormat": null,
"enumValues": null,
"schemaId": "string",
"multivalued": false,
"uiHints": {},
"computedAssemblyProperty": false,
"computedProperty": false,
"propertyOverrideStatus": 0
}]
}
- Call updateWVEPMetadata. Replace the
{propertyId}
value with the ID from the last step, and replacevalue
with your desired part number. See Release Management for steps on getting the next valid available part number to use.curl -X 'POST' \ 'https://cad.onshape.com/api/v10/metadata/d/{did}/w/{wid}/e/{eid}/p/{pid}' \ -H 'accept: application/json;charset=UTF-8; qs=0.09' \ -H 'Authorization: Basic CREDENTIALS' \ -H 'Content-Type: application/json;charset=UTF-8; qs=0.09' \ -d '{ "jsonType": "metadata-part", "partId": "{pid}", "properties": [ { "value": "PRT-002", "propertyId": "{propertyId}" } ] }'
import requests import json auth = (access_key, secret_key) # See Authentication guide headers = { 'Accept': 'application/json;charset=UTF-8;qs=0.09', 'Content-Type': 'application/json;charset=UTF-8; qs=0.09' } api_url = "https://{baseUrl}.onshape.com/api/v10/metadata/d/{did}/w/{wid}/e/{eid}/p/{pid}" queryParams = {} body = { "jsonType": "metadata-part", "partId": "{pid}", "properties": [ { "value": "PRT-002", "propertyId": "{propertyId}" } ] } response = requests.post( api_url, params=queryParams, json=body, auth=auth, headers=headers ) print(json.dumps(response.json(), indent=4))
- Repeat steps 2 and 3 to confirm that the
value
for the part is nowPRT-002
.
Update a tab name
In this example we will update an element’s tab name by getting the current metadata for the element, and then posting an update to that metadata. Remember that in Onshape, an element is typically represented as a tab in the Onshape UI.
Endpoint
POST api/v10/metadata/d/{did}/w/{wid}/e/{eid}
Request Body
{
"properties": [
{
"value": "",
"propertyId": "{propertyId}"
}
]
}
Make a copy of this public document. Make a note of the new document’s document ID, workspace ID, and element ID. Note the tab name of the element is “NEW_PART”.
To get the current metadata of the element, call the getWMVEMetadata endpoint to get the current metadata JSON for the element. Don’t forget to replace the URL parameters with the IDs from your new document, and replace
CREDENTIALS
with your authorization credentials.curl -X 'GET' \ 'https://cad.onshape.com/api/v10/metadata/d/{did}/w/{wid}/e/{eid}' \ -H 'accept: application/json;charset=UTF-8; qs=0.09' \ -H 'Authorization: Basic CREDENTIALS' \ -H 'Content-Type: application/json;charset=UTF-8; qs=0.09'
import requests import json auth = (access_key, secret_key) # See Authentication guide headers = { 'Accept': 'application/json;charset=UTF-8;qs=0.09', 'Content-Type': 'application/json;charset=UTF-8; qs=0.09' } api_url = "https://{baseUrl}.onshape.com/api/v10/metadata/d/{did}/w/{wid}/e/{eid}" queryParams = {} response = requests.get( api_url, params=queryParams, auth=auth, headers=headers ) print(json.dumps(response.json(), indent=4))
The call returns a response body in JSON format. Scroll to the
Name
properties block of the JSON response, and notice that thevalue
field matches our current tab name, “NEW_PART”.{ "jsonType": "metadata-element", "elementType": 0, "mimeType": "onshape/partstudio", "elementId": "{eid}", "properties": [ { "name": "Name", "value": "NEW_PART", "validator": {}, "required": true, "editable": true, "propertyId": "{propertyId}", // .. }, // ... ], // ... }
Copy the Name block’s
propertyId
in the response. We’ll need this ID to update the metadata.Set up the updateWVEMetadata call.
Note that we need to include the
propertyId
and the value to update. In the request JSON, replace{propertyId}
with the property ID you found in the last step, then change the emptyvalue
string to"PISTON"
:curl -X 'POST' \ 'https://cad.onshape.com/api/v10/metadata/d/{did}/w/{wid}/e/{eid}' \ -H 'accept: application/json;charset=UTF-8; qs=0.09' \ -H 'Authorization: Basic CREDENTIALS' \ -H 'Content-Type: application/json;charset=UTF-8; qs=0.09' \ -d '{ "properties": [ { "value": "PISTON", "propertyId": "{propertyId}" } ] }'
import requests import json auth = (access_key, secret_key) # See Authentication guide headers = { 'Accept': 'application/json;charset=UTF-8;qs=0.09', 'Content-Type': 'application/json;charset=UTF-8; qs=0.09' } api_url = "https://{baseUrl}.onshape.com/api/v10/metadata/d/{did}/w/{wid}/e/{eid}" queryParams = {} body = { "properties": [ { "value": "PISTON", "propertyId": "{propertyId}" } ] } response = requests.post( api_url, params=queryParams, json=body, auth=auth, headers=headers ) print(json.dumps(response.json(), indent=4))
Open your document and confirm that the tab name is now
PISTON
.
Update standard content part metadata
Standard Content Notes
|
---|
Endpoint
POST api/v10/metadata/standardcontent/d/{did}
Request Body
{
"items": [
{
"href": "https://{baseUrl}.onshape.com/api/v10/metadata/standardcontent/d/{did}/v/{vid}/e/{eid}/c/{companyId}/p/{pid}?configuration={config}&linkDocumentId={linkDocumentId}&applyToAllConfigs=true",
"properties": [
{
"value": "",
"propertyId": "{propertyId1}"
},
{
"value": "",
"propertyId": "{propertyId2}"
}
]
}
]
}