Python

Start integrating any APIs with the Bearer client for Python

Installing

Python
pip install bearer

Initializing

Python
# Initialize the client with your Bearer secret key
from bearer import Bearer
bearer = Bearer(BEARER_SECRET_KEY)

Replace the BEARER_SECRET_KEY with your Secret Key that can be found on your Developer Keys page.

Calling APIs

Once you've added an API through the Bearer dashboard, you can make request to it as follow:

# GET request to the GitHub API
bearer \
.integration('github') \
.get('/repositories', { query: { since: 364 } })

Most common HTTP methods are supported out-of-the-box, including .get() , .post(), .put()and.delete().

To pass parameters, construct your request as follow:

bearer.integration('github') \
.get('/endpoint', { \
query: { 'foo': 'bear'}, \ # your query parameters as key/value
body: {}, \ # your body as key/value or string
headers: { 'foo': 'bear' } \ # your headers as key/value
})

Credentials management

By default, all requests will be authenticated using the latest credentials saved into your account for that API. Thus, changing the API credentials in your account will be instantly reflected into your code.

If you want to use custom credentials, for example your customers credentials, first retrieve them on your frontend with the setup component, then update your code to use the .setup() method as follow:

// GET request to the GitHub API with custom credentials
// (retrieve a setupId with the Bearer setup component)
bearer.integration('github')
.setup(setupId)
.get("/repositories", { query: { since: 364 } })
.then(({ data }) => { console.log(data) });

Dealing with OAuth APIs

When the API you want to use rely on OAuth as an authentication method, Bearer will handle the OAuth dance for you.

Just add a connect button to your frontend to let your users connect to the service. Then update your code to use the .auth() method as follow:

# POST request using an authId (only for OAuth APIs)
# (generate an authId with the Bearer connect button)
bearer.integration('github') \
.auth(authId) \
.post('/user/repos', { body: { name: 'Just setting up my Bearer.sh' } })

Request timeout

The Python client has a 5 seconds timeout per default. After that period of time, the HTTP connection will be closed by the client.

# increase the request timeout to 10 seconds globally
bearer = Bearer('BEARER_SECRET_KEY', http_client_settings={"timeout": 10})
# you can specify client settings per integration as well
github = bearer.integration('github', http_client_settings={"timeout": 2})

You can increase the timeout up to 30 seconds, but not more, as it's the limit of our servers.

Tracking requests

The Bearer Python client allows you to set a correlationId to make logs grouping easier. This can be done by using the special Bearer-Correlation-Id header.

github.get('/user/repos', headers={ 'Bearer-Correlation-Id': 'your-correlation-id' })

On the Bearer dashboard, you can then filter the logs using your-correlation-id.

Client reference

This page contains the most common use case for the Bearer API client in Node.js.

If you are trying to achieve something not listed here, have a look at our Python client reference on the respective repository on GitHub. It includes details about the functions, classes, return types, arguments and more. As well as the source code of the client.