Here's some code that might help you out.

Examples:

Example 1 (auth):

username = 'user'
token = 'token'

login = requests.get('https://api.github.com/search/repositories?q=github+api', auth=(username,token))

Example 2 (headers):

headers = {'Authorization': 'token ' + token}

login = requests.get('https://api.github.com/user', headers=headers)
print(login.json())

Example 3 (delete repo):

user = 'username'
repo = 'some_repo' # Delete this repo

headers = {'Authorization': 'token ' + token}

login = requests.delete('https://api.github.com/' + 'repos/' + user + '/' + repo, headers=headers)

Example 4 (create repo):

repo = 'some_repo'
description = 'Created with api'

payload = {'name': repo, 'description': description, 'auto_init': 'true'}

login = requests.post('https://api.github.com/' + 'user/repos', auth=(user,token), data=json.dumps(payload))

You might want to take a look at the following docs:

Requests Docs

Github API docs

I hope this helps.

Answer from user6927293 on Stack Overflow
🌐
GitHub
github.com › WilliamQLiu › python-examples › blob › master › requests › example_requests.py
python-examples/requests/example_requests.py at master · WilliamQLiu/python-examples
"User-Agent": "python-requests/2.5.3 CPython/2.7.9 Darwin/14.1.0" }, "json": null, "origin": "74.71.230.126", "url": "http://httpbin.org/post" } """ · · def pass_headers_in_request(): """ Add HTTP headers to a request by adding a dict to the 'headers' param · """ url = 'https://api.github.com/some/endpoint' payload = { 'some': 'data' } headers = { 'content-type': 'application/json' } r = requests.post(url, data=json.dumps(payload), headers=headers) print r ·
Author   WilliamQLiu
🌐
GitHub
github.com › requests
requests · GitHub
requests/toolbelt’s past year of commit activity ... An authentication handler for using Kerberos with Python Requests.
🌐
GitHub
github.com › psf › requests
GitHub - psf/requests: A simple, yet elegant, HTTP library. · GitHub
Requests officially supports Python 3.10+.
Starred by 53.9K users
Forked by 9.8K users
Languages   Python 99.3% | Makefile 0.7%
🌐
GitHub
github.com › luminati-io › python-requests
GitHub - luminati-io/python-requests: Python's 'requests' library: learn HTTP methods, parsing responses, proxy usage, timeouts, and more for efficient web scraping. · GitHub
They provide extra information to the server about the request, usually on how to filter data and customize the response. ... In this example, ?key1=value1&key2=value2 is the query string while key1 and key2 are query parameters.
Author   luminati-io
🌐
GitHub
github.com › basdijkstra › requests-workshop
GitHub - basdijkstra/requests-workshop: Slides, examples, exercises and answers for my open source workshop on API testing in Python using requests · GitHub
Slides, examples, exercises and answers for my open source workshop on API testing in Python using requests - basdijkstra/requests-workshop
Starred by 95 users
Forked by 44 users
Languages   Python
🌐
GitHub
github.com › topics › python-requests
python-requests · GitHub Topics · GitHub
Requests 3.0, for Humans and Machines, alike. 🤖 · http http-client python3 requests forhumans kennethreitz python-requests requests-module
Top answer
1 of 5
51

Here's some code that might help you out.

Examples:

Example 1 (auth):

username = 'user'
token = 'token'

login = requests.get('https://api.github.com/search/repositories?q=github+api', auth=(username,token))

Example 2 (headers):

headers = {'Authorization': 'token ' + token}

login = requests.get('https://api.github.com/user', headers=headers)
print(login.json())

Example 3 (delete repo):

user = 'username'
repo = 'some_repo' # Delete this repo

headers = {'Authorization': 'token ' + token}

login = requests.delete('https://api.github.com/' + 'repos/' + user + '/' + repo, headers=headers)

Example 4 (create repo):

repo = 'some_repo'
description = 'Created with api'

payload = {'name': repo, 'description': description, 'auto_init': 'true'}

login = requests.post('https://api.github.com/' + 'user/repos', auth=(user,token), data=json.dumps(payload))

You might want to take a look at the following docs:

Requests Docs

Github API docs

I hope this helps.

2 of 5
39

For one, I would recommend using a wrapper for the API. You're asking a lot of questions on here that could be simplified by finding a wrapper whose API you appreciate. There's a list of wrappers written in Python here.

As for your actually answering your question, the GitHub documentation is fairly clear that you need to send the Authorization header. Your call would actually look like this:

self.headers = {'Authorization': 'token %s' % self.api_token}
r = requests.post(url, headers=self.headers)

Since it seems like you're using requests and a class, might I be so bold as to make a recommendation? Let's say you're doing something like making a client for the API. You might have a class like so:

class GitHub(object):
    def __init__(self, **config_options):
        self.__dict__.update(**config_options)
        self.session = requests.Session()
        if hasattr(self, 'api_token'):
           self.session.headers['Authorization'] = 'token %s' % self.api_token
        elif hasattr(self, 'username') and hasattr(self, 'password'):
           self.session.auth = (self.username, self.password)

    def call_to_the_api(self, *args):
        # do stuff with args
        return self.session.post(url)

The Session object will take care of the authentication for you (either by the tokens or username and password combination).

Also, if you end up deciding to use github3.py for your API wrapper needs, there's a tag on here for it.

🌐
PyPI
pypi.org › project › requests
requests · PyPI
Requests officially supports Python 3.10+. Requests is ready for the demands of building robust and reliable HTTP–speaking applications, for the needs of today. ... When cloning the Requests repository, you may need to add the -c fetch.fsck.badTimezone=ignore flag to avoid an error about a bad commit timestamp (see this issue for more background): git clone -c fetch.fsck.badTimezone=ignore https://github.com/psf/requests.git
      » pip install requests
    
Published   Mar 25, 2026
Version   2.33.0
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 64430181 › how-to-access-the-github-api-via-requests-in-python
How to access the github API via requests in python? - Stack Overflow
url = "https://api.github.com/repos/rsapkf/42/commits" headers = {"Accept": "application/vnd.github.inertia-preview+json"} my_username = "someuser" my_token = "a0d42faabef23ab5b5462394373fc133ca107890" r = requests.get(url, headers=headers, ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-requests-tutorial
Python Requests - GeeksforGeeks
July 31, 2025 - Example: Let's try making a request to github's APIs for example purposes.
🌐
GitHub
github.com › topics › requests-python
requests-python · GitHub Topics · GitHub
python python3 requests api-wrapper hypixel hypixel-api hypixel-skyblock requests-python ... POST METHOD 101 is a simple example of communication between server API and client using Fastapi and Streamlit
🌐
Vivitoa
vivitoa.github.io › python-cheat-sheet › pages › api-requests.html
Python API & Requests - Python Cheat Sheet
The requests library is not part ... · The requests library provides simple methods for different HTTP methods: import requests # GET request response = requests.get('https://api.example.com/data') # POST request response = requests.post('https://api.example.com/submit', ...
🌐
GitHub
github.com › klchang › python-requests-examples
GitHub - klchang/python-requests-examples: Examples with python requests library
Examples with python requests library. Contribute to klchang/python-requests-examples development by creating an account on GitHub.
Author   klchang
🌐
GitHub
github.com › psf › requests › blob › main › README.md
requests/README.md at main · psf/requests
Requests officially supports Python 3.10+. Requests is ready for the demands of building robust and reliable HTTP–speaking applications, for the needs of today. ... When cloning the Requests repository, you may need to add the -c fetch.fsck.badTimezone=ignore flag to avoid an error about a bad commit timestamp (see this issue for more background): git clone -c fetch.fsck.badTimezone=ignore https://github.com/psf/requests.git
Author   psf
🌐
GitHub
github.com › kennethreitz › requests
GitHub - kennethreitz/requests: A simple, yet elegant HTTP library.
Requests officially supports Python 3.8+.
Starred by 308 users
Forked by 98 users
Languages   Python 99.3% | Makefile 0.7% | Python 99.3% | Makefile 0.7%
🌐
Martin Heinz
martinheinz.dev › blog › 25
All the Things You Can Do With GitHub API and Python | Martin Heinz | Personal Website & Blog
June 15, 2020 - The code above uses json.dumps() to convert Python dictionary to JSON string to create request body and the usual Authorization header. Below you can see the relevant parts of the expected response: {"comments": 0, "description": null, "files": {"code.py": {"content": "print('some code')", "filename": "code.py", "language": "Python", "raw_url": "https://gist.githubusercontent.com/MartinHeinz/.../raw/8d53df5862f8b687fc09d0b3c1b3c49afe441cbe/code.py", "size": 18, "truncated": null, "type": "application/x-python"}}, "forks": [], "html_url": "https://gist.github.com/383c6b450f892e169074a642a372e459", "id": "383c6b450f892e169074a642a372e459", "node_id": "MDQ6R2lzdDM4M2M2YjQ1MGY4OTJlMTY5MDc0YTY0MmEzNzJlNDU5", "public": true, "url": "https://api.github.com/gists/383c6b450f892e169074a642a372e459" }
🌐
GitHub
github.com › PyGithub › PyGithub
GitHub - PyGithub/PyGithub: Typed interactions with the GitHub API v3 · GitHub
Long-term discussion and bug reports are maintained via GitHub Issues. Code review is done via GitHub Pull Requests.
Starred by 7.7K users
Forked by 1.9K users
Languages   Python 99.6% | Shell 0.4%
🌐
GitHub
github.com › requests › requests-oauthlib
GitHub - requests/requests-oauthlib: OAuthlib support for Python-Requests! · GitHub
OAuthlib support for Python-Requests! Contribute to requests/requests-oauthlib development by creating an account on GitHub.
Starred by 1.8K users
Forked by 425 users
Languages   Python
🌐
Medium
medium.com › analytics-vidhya › getting-started-with-github-api-dc7057e2834d
Getting Started with Github API. REST API v3 using Python | by Gaganpreet Kaur Kalsi | Analytics Vidhya | Medium
July 20, 2020 - Example JSON → data = {“name” : “Hello-repo”} NOTE : — Be careful while constructing a URL and with the type of request. These were mostly the areas where the API threw an error to me.
🌐
Python-requests
docs.python-requests.org › en › latest › user › quickstart
Quickstart — Requests 2.33.0 documentation
>>> import requests >>> r = requests.get('https://api.github.com/events') >>> r.json() [{'repository': {'open_issues': 0, 'url': 'https://github.com/... In case the JSON decoding fails, r.json() raises an exception. For example, if the response gets a 204 (No Content), or if the response contains ...