Pinterest API SDK

0.2.6 · active · verified Thu Apr 16

The Pinterest API SDK is a Python library designed to simplify interaction with the Pinterest API v5. It provides functionalities for campaign management, streamlines authentication, and handles error reporting. The SDK is actively developed, with its current version being 0.2.6, and aims to expand its capabilities to cover organic Pins, shopping, analytics, and more over time.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Pinterest SDK client using environment variables and retrieve a list of accessible ad accounts. Ensure your Pinterest App ID, Secret, and an Access Token (or Refresh Token) are configured as environment variables or in a `.env` file for the `create_default_client()` method to function.

import os
from pinterest.client import PinterestSDKClient
from pinterest.ads.ad_accounts import AdAccount

# Set environment variables or create a .env file with PINTEREST_APP_ID, PINTEREST_APP_SECRET,
# PINTEREST_ACCESS_TOKEN (or PINTEREST_REFRESH_ACCESS_TOKEN if using refresh token flow).
# Example environment variables:
# os.environ['PINTEREST_APP_ID'] = 'YOUR_APP_ID'
# os.environ['PINTEREST_APP_SECRET'] = 'YOUR_APP_SECRET'
# os.environ['PINTEREST_ACCESS_TOKEN'] = 'YOUR_ACCESS_TOKEN'

# Create a default client (uses environment variables or config.json)
# For production, consider using create_client_with_refresh_token for long-lived access
try:
    client = PinterestSDKClient.create_default_client()
    
    # Example: List ad accounts
    ad_accounts = AdAccount.get_all(client=client) # Pass client explicitly if not using default behavior
    
    if ad_accounts:
        print(f"Found {len(ad_accounts)} ad account(s).")
        for account in ad_accounts:
            print(f"  Ad Account ID: {account.id}, Name: {account.name}")
    else:
        print("No ad accounts found or accessible.")

except Exception as e:
    print(f"An error occurred: {e}")
    print("Please ensure your Pinterest API credentials (APP_ID, APP_SECRET, ACCESS_TOKEN) are correctly set as environment variables or in a .env file.")

view raw JSON →