Skip to main content

API wrapper for Instagram APIs

Project description

Pystagram logo

HomeDocsPyPiFeaturesInstallationUsageContributors

Python Version Python Version GitHub Release PyPI PyPI Downloads PyPI License

Pystagram is a python client for Instagram APIs.

It provides a simple and easy to use interface for accessing endpoints of both Graph and Basic Display APIs.

Features

  • Instagram Graph API:
    • get and publish instagram media (posts, stories, reels)

    • manage and reply to comments

    • perform hashtag searches

    • get metrics about Instagram Businesses and Creators.

  • Instagram Basic Display API:
    • get user profile information

    • get user media

Installation

PyPI (Python)

PyPI

pip install pystagram

Source Code (Github)

GitHub Release

git clone https://github.com/MatthieuThib/pystagram.git
cd pystagram
pip install .

Usage

Setup account

In order to use the Instagram APIs (Graph API and Basic Display API), some prerequisites are required, follow the getting started guide to set up your account and get the necessary credentials:

This will provide you with the following credentials:

  • App ID

  • App Secret

  • Access Token

Instagram APIs use access tokens to authenticate requests. Those tokens are tied to specific permissions and can be generated for different purposes. Before calling any endpoint, make sure that the access token has the necessary permissions to request the endpoint.

See the Permissions page for more information.

Code examples

Instagram Graph API

Publishing a media

import os

# Importing the necessary modules
from pystagram import PystagramGraphApi
from pystagram.components.containers import ImageContainer

# Initializing the PystagramGraphApi with the necessary credentials
graph_api = PystagramGraphApi(
    app_id=int(os.getenv("APP_ID")),  # The App ID from the environment variables
    app_secret=os.getenv("APP_SECRET"),  # The App Secret from the environment variables
    access_token=os.getenv("ACCESS_TOKEN"),  # The Access Token from the environment variables
)

# Creating an ImageContainer with the image URL and caption
container = ImageContainer(
    image_url="https://www.example.com/image.jpg",  # The URL of the image
    caption="your caption #hashtag",  # The caption for the image
    # Additional parameters can be added here
)

# Creating a media object with the ImageContainer
response = graph_api.user.media.create(container)
# Extracting the ID of the created media object
container_id = response.data.get("id")

# Publishing the created media object
graph_api.user.media_publish.create(container_id=container_id)

Instagram Basic Display API

Fetch user media

import os

from pystagram import PystagramBasicDisplayApi
from pystagram.components.fields import MediaFields

# Instantiate the PystagramBasicDisplayApi class with the necessary credentials
basic_display_api = PystagramBasicDisplayApi(
    app_id=int(os.getenv("APP_ID")),  # The App ID from the environment variables
    app_secret=os.getenv("APP_SECRET"),  # The App Secret from the environment variables
    access_token=os.getenv("ACCESS_TOKEN"),  # The Access Token from the environment variables
)

# Fetch the user's media from the API
# The get() method sends a GET request to the API and returns the response
response = basic_display_api.user.user_media.get()

# Extract the user's media data from the response
user_media = response.data.get("data")

Paginated endpoints

Both APIs feature paginated endpoints, which means that the response of a request can be split into multiple pages. The pystagram library handles this by decorating the endpoints’ methods with a custom decorator @cursor_paginated. When called, the decorated method will iterate over all the pages until there is no more pages to fetch or the maximum number of pages is reached. By default, the maximum number of pages is set to None (ie. no limit), but it can be changed by passing setting the attribute MAX_PAGES of the class to a different integer value.

from pystagram import PystagramGraphApi

# Initializing the PystagramGraphApi with the necessary credentials
graph_api = PystagramGraphApi( ... )

# Set the maximum number of pages to fetch from the API
graph_api.MAX_PAGES = 5

# Request a cursor paginated endpoint
response = graph_api.user.media.get()

Contributors

Contributors

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pystagram-1.1.0.tar.gz (3.3 MB view details)

Uploaded Source

Built Distribution

pystagram-1.1.0-py3-none-any.whl (76.3 kB view details)

Uploaded Python 3

File details

Details for the file pystagram-1.1.0.tar.gz.

File metadata

  • Download URL: pystagram-1.1.0.tar.gz
  • Upload date:
  • Size: 3.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for pystagram-1.1.0.tar.gz
Algorithm Hash digest
SHA256 6f5ed9c5dbad875e1f0f36fce49e7e30593c98f9517c797ae293777353eae906
MD5 e585c411757de4ec56887f632449fa8d
BLAKE2b-256 877a14383422d6e5634b0561e0a5e89ebe97af4f81a82e4c9a98c0e75660b09c

See more details on using hashes here.

File details

Details for the file pystagram-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: pystagram-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 76.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for pystagram-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 774a65ef66f2d2dc13e4b49c0766ef0be1b474ab9252622414c3b89ee0bdeddc
MD5 248b5befbdc4577c9bdd2c69e4f55d4e
BLAKE2b-256 89d264eb164d3e5327f6e90a204ff26300b3351eb067de823fe1abccfaa8a99d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page