Examples

This section provides practical examples of how to use the openPIM API in common programming languages. These examples cover common use cases, including authentication, querying data, and handling errors.


GraphQL Query

JavaScript (NodeJS)

const axios = require('axios');

const API_TOKEN = 'your token'

const query = `query {
    getProducts(GetProductsInput: {
        product_ids: ["6647c18f1294fb312eb1cf97"]
    }) {
        name_universal
        title {
            short
        }
        primary_media {
            image {
                image_id
                alt_text
            }
        }
    }
}`

const queryOpenPIM = async () => {
    try {
        const response = await axios.post('https://api.openpim.io/v1/graphql', {'query': query}, {
            headers: {
                'Authorization': API_TOKEN,
                'Content-Type': 'application/json'
            }
        });

        // Check for any GraphQL errors
        if (response.errors) {
            console.error('GraphQL Errors:', response.errors);
        } else {
            console.log('GraphQL Response Data:', response.data);
        }
    } catch (error) {
        console.error('Error making GraphQL request:', error);
    }
};

queryOpenPIM();

Python

import requests

API_TOKEN = 'your token'

query = '''
query {
    getProducts(GetProductsInput: {
        product_ids: ["6647c18f1294fb312eb1cf97"]
    }) {
        name_universal
        title {
            short
        }
        primary_media {
            image {
                image_id
                alt_text
            }
        }
    }
}
'''

def queryOpenPIM():
    try:
        response = requests.post('https://api.openpim.io/v1/graphql', json={'query': query}, headers={
            'Authorization': API_TOKEN
        })
        response.raise_for_status()  # Raise an error for 4xx or 5xx status codes
        response = response.json()  # Parse response JSON

        if 'errors' in response:
            print('GraphQL Errors:', response['errors'])
        else:
            print('GraphQL Data:', response['data'])
    except requests.exceptions.RequestException as e:
        print('Error making GraphQL request:', e)

queryOpenPIM()

REST API Request

JavaScript (NodeJS)

const axios = require('axios');

const API_TOKEN = 'your token'
const IMAGE_ID = '6647c18f1294fb312eb1cf97'

const fetchImage = async () => {
    try {
        const response = await axios.get(`https://api.openpim.io/v1/image/${IMAGE_ID}?resolution=medres&ratio=sq`,
        {
            responseType: 'arraybuffer',
            headers: {
                'Authorization': API_TOKEN
            }
        });

        // Do something with the response
        
    } catch (error) {
        console.error('Error fetching the image:', error);
    }
};

fetchImage();

Python

import requests

API_TOKEN = 'your token'
IMAGE_ID = '6647c18f1294fb312eb1cf97'

def fetchImage():
    try:
        response = requests.get(f'https://api.openpim.io/v1/image/{IMAGE_ID}?resolution=medres&ratio=sq', headers={
            'Authorization': API_TOKEN
        }, stream=True)
        response.raise_for_status()  # Raise an error for 4xx or 5xx status codes

        # Do something with the response
        
    except requests.exceptions.RequestException as e:
        print('Error fetching the image:', e)

fetchImage()

Next we will cover: