In this example, we will call an Onshape REST API endpoint to send a document name to our console. We highly recommend completing the Learning Center’s Intro to the Onshape API course and mastering using Onshape APIs within the Glassworks Explorer before moving on to creating scripts and apps. Please note that the sample shown on this page is only designed to be used as a quick start guide and does not represent a full Onshape application.
import requests
import json
# Assemble the URL for the API call
api_url = "https://cad.onshape.com/api/v10/documents/e60c4803eaf2ac8be492c18e"
# Optional query parameters can be assigned
params = {}
# Use the keys from the developer portal
access_key = "ACCESS_KEY"
secret_key = "SECRET_KEY"
# Define the header for the request
headers = {'Accept': 'application/json;charset=UTF-8;qs=0.09',
'Content-Type': 'application/json'}
# Putting everything together to make the API request
response = requests.get(api_url,
params=params,
auth=(access_key, secret_key),
headers=headers)
# Convert the response to formatted JSON and print the `name` property
print(json.dumps(response.json()["name"], indent=4))
curl -X 'GET' \
https://cad.onshape.com/api/v10/documents/e60c4803eaf2ac8be492c18e \
-H 'Content-Type: application/json' \
-H 'Accept: application/json;charset=UTF-8; qs=0.09' \
-H 'Authorization: Basic CREDENTIALS'
import fetch from 'node-fetch';
async function getDocument(url='') {
const response = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json;charset=UTF-8;qs=0.09',
Authorization: `Basic ${btoa('CREDENTIALS')}`
}
});
return response.json();
}
getDocument('https://cad.onshape.com/api/v10/documents/e60c4803eaf2ac8be492c18e'
).then((data) => {
console.log(data.name);
});
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
int main() {
string url = "curl ";
url += "-X 'GET' ";
url += "'https://cad.onshape.com/api/v10/documents/e60c4803eaf2ac8be492c18e'";
url += "-H 'accept: application/json;charset=UTF-8; qs=0.09' ";
url += "-H 'Authorization: Basic CREDENTIALS'";
system(url.c_str());
return 0;
}
Watch the Onshape Quick Start video as you follow along with the steps on this page.
Document
.GET /documents/{did}
endpoint. Note that in the URL, the name of this API is getDocument
.Media Type
that we’ll need to include in our header.Navigate to this public document, and make a note of the document ID in the URL (e60c4803eaf2ac8be492c18e
).
API keys
.Create new API key
button.Application can read your documents.
Application can write to your documents.
Create API key
button.Close
button.IMPORTANT NOTE: You will not be able to find the secret key again, so save it somewhere safe!ACCESS_KEY
and SECRET_KEY
in this example.ACCESS_KEY
and SECRET_KEY
in the code examples below, or you’ll see CREDENTIALS
to specify that the keys must be encoded before use. See API Guide: API Keys for details.Follow along with the steps in this section to create the final code. This example uses python, but you can refer to the code snippets at the top of this page for other languages.
getDocInfo.py
.import requests
import json
# Assemble the URL for the API call
api_url = "ASSEMBLED_URL"
ASSEMBLED_URL
with the fully formed API. This is where we’ll put together everything we’ve learned so far:https://cad.onshape.com/api
https://companyName.onshape.com/api
for Enterprise accountsgetDocument
API in Glassworks: /documents/{did}
{did}: e60c4803eaf2ac8be492c18e
https://cad.onshape.com/api/v10/documents/e60c4803eaf2ac8be492c18e
# Optional query parameters can be assigned
params = {}
ACCESS_KEY
and SECRET_KEY
with your strings from the dev portal.# Use the keys from the developer portal
access_key = "ACCESS_KEY"
secret_key = "SECRET_KEY"
# Define the header for the request
headers = { 'Accept': 'MEDIA_TYPE',
'Content-Type': 'application/json'
}
MEDIA_TYPE
with the Media type we obtained from the API Explorer during the Review the API section above:'Accept': 'application/json;charset=UTF-8;qs=0.09',
# Put everything together to make the API request
response = requests.get(api_url,
params=params,
auth=(access_key, secret_key),
headers=headers)
# Convert the response to formatted JSON and print the `name` property
print(json.dumps(response.json()["name"], indent=4))
getDocInfo.py
file:cd ~/<your-path>
cd \<your-path>
pip
installed on your machine, you can run pip install requests
instead.python3 -m pip install requests
py -m pip install requests
python3 getDocInfo.py
py getDocInfo.py
"Onshape API Guide"