Skip to main content
Pystagram logo

Home • Docs • PyPi • Features • Installation • Usage • Contributors

Python Version Python Version GitHub Release PyPI PyPI Downloads PyPI GitHub Package GitHub Package 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

Metadata

Release files for pystagram 1.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pystagram 1.2.0
File Size Uploaded
pystagram-1.2.0.tar.gz 3.3 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for pystagram 1.2.0
File Interpreter ABI Platform
pystagram-1.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 3.4 MB

Release files / pystagram-1.2.0.tar.gz

Download URL pystagram-1.2.0.tar.gz
Size 3.3 MB
Tags Source
SHA-256 checksum
How to use checksums
8c82cc99fa904a15c6f93d08bc73371a113d74004aafcae3dc5dff5d54ae8619
BLAKE2b-256 checksum
How to use checksums
7344c024b1479084983fbe7fdebaf1bfb818be7a60ca229183781860fc27d5b4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.2

Release files / pystagram-1.2.0-py3-none-any.whl

Download URL pystagram-1.2.0-py3-none-any.whl
Size 76.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
684b8f8bfb5679d81aa4d4e247dd41545415e83a93cce12c166f1f90fbdb8340
BLAKE2b-256 checksum
How to use checksums
b8fefaac4988864a3057d569d33ea2c893dd014c8a7f30d4196100b9a87a103c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.2

Release history Release notifications | RSS feed

This release

1.2.0 This release

2 release files

1.1.0

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2

2 release files

0.1.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page