Skip to main content

Official Python SDK for the Apifreaks API platform.

Project description

Apifreaks Python SDK

fern shield pypi shield python shield

The Apifreaks Python library provides convenient access to the Apifreaks APIs from Python applications.

Table of Contents

Installation

Install the package from PyPI:

pip install apifreaks

Or add it with Poetry:

poetry add apifreaks

Or add it with uv:

uv add apifreaks

The package supports Python >=3.8.

Reference

A full reference for this library is available here.

Usage

Instantiate and use the client with the following:

import os

from apifreaks import ApifreaksApi
from apifreaks.core.api_error import ApiError

client = ApifreaksApi()

try:
    response = client.geolocation_lookup(
        api_key=os.environ["APIFREAKS_API_KEY"],
        ip="8.8.8.8",
    )
    print(response)
except ApiError as e:
    print(e.status_code)
    print(e.body)

Example domain WHOIS lookup:

import os

from apifreaks import ApifreaksApi
from apifreaks.core.api_error import ApiError

client = ApifreaksApi()

try:
    response = client.domain_whois_lookup(
        api_key=os.environ["APIFREAKS_API_KEY"],
        domain_name="example.com",
    )
    print(response)
except ApiError as e:
    print(e.status_code)
    print(e.body)

Example bulk geolocation lookup:

import os

from apifreaks import ApifreaksApi

client = ApifreaksApi()

response = client.bulk_geolocation_lookup(
    api_key=os.environ["APIFREAKS_API_KEY"],
    ips=["8.8.8.8", "1.1.1.1"],
)

print(response)

Async Usage

The SDK also provides an async client:

import asyncio
import os

from apifreaks import AsyncApifreaksApi
from apifreaks.core.api_error import ApiError

async def main() -> None:
    client = AsyncApifreaksApi()

    try:
        response = await client.geolocation_lookup(
            api_key=os.environ["APIFREAKS_API_KEY"],
            ip="8.8.8.8",
        )
        print(response)
    except ApiError as e:
        print(e.status_code)
        print(e.body)

asyncio.run(main())

Environments

This SDK allows you to configure the API environment for requests.

from apifreaks import ApifreaksApi
from apifreaks.environment import ApifreaksApiEnvironment

client = ApifreaksApi(
    environment=ApifreaksApiEnvironment.DEFAULT,
)

You can also override the base URL directly:

from apifreaks import ApifreaksApi

client = ApifreaksApi(
    base_url="https://api.apifreaks.com",
)

Errors

When the API returns a non-success status code, the SDK raises an API error.

from apifreaks import ApifreaksApi
from apifreaks.core.api_error import ApiError

client = ApifreaksApi()

try:
    response = client.geolocation_lookup(
        api_key="your_api_key",
        ip="8.8.8.8",
    )
    print(response)
except ApiError as e:
    print(f"Status code: {e.status_code}")
    print(f"Response body: {e.body}")

Request Types

The SDK exports generated request and response types from the apifreaks package.

from apifreaks import GeolocationLookupResponse

# Returned from client.geolocation_lookup(...)
response: GeolocationLookupResponse

Most endpoints accept keyword arguments directly, so you usually do not need to manually instantiate request objects.

Advanced

Retries

The SDK is instrumented with automatic retries. By default, failed requests are retried up to 2 times when the request is considered retryable.

You can configure retries globally on the client:

from apifreaks import ApifreaksApi

client = ApifreaksApi(max_retries=3)

You can also configure retries for a single request:

from apifreaks import ApifreaksApi

client = ApifreaksApi()

response = client.geolocation_lookup(
    api_key="your_api_key",
    ip="8.8.8.8",
    request_options={"max_retries": 3},
)

Timeouts

The SDK defaults to a 60 second timeout.

Configure the timeout globally:

from apifreaks import ApifreaksApi

client = ApifreaksApi(timeout=30)

Configure the timeout for a single request:

from apifreaks import ApifreaksApi

client = ApifreaksApi()

response = client.geolocation_lookup(
    api_key="your_api_key",
    ip="8.8.8.8",
    request_options={"timeout_in_seconds": 30},
)

Additional Headers

You can add custom headers to a request using request_options.

from apifreaks import ApifreaksApi

client = ApifreaksApi()

response = client.geolocation_lookup(
    api_key="your_api_key",
    ip="8.8.8.8",
    request_options={
        "additional_headers": {
            "X-Custom-Header": "custom-value",
        },
    },
)

Additional Query String Parameters

You can add custom query parameters using request_options.

from apifreaks import ApifreaksApi

client = ApifreaksApi()

response = client.geolocation_lookup(
    api_key="your_api_key",
    ip="8.8.8.8",
    request_options={
        "additional_query_parameters": {
            "filter": "active",
        },
    },
)

Raw Responses

Use with_raw_response when you need access to raw response metadata.

from apifreaks import ApifreaksApi

client = ApifreaksApi()

response = client.with_raw_response.geolocation_lookup(
    api_key="your_api_key",
    ip="8.8.8.8",
)

print(response.status_code)
print(response.headers)
print(response.data)

Contributing

While we value open-source contributions to this SDK, this library is generated programmatically. Additions made directly to this library would have to be moved over to our generation code, otherwise they would be overwritten upon the next generated release. Feel free to open a PR as a proof of concept, but know that we will not be able to merge it as-is. We suggest opening an issue first to discuss with us!

On the other hand, contributions to the README are always very welcome!

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

apifreaks-1.0.0.tar.gz (267.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

apifreaks-1.0.0-py3-none-any.whl (624.5 kB view details)

Uploaded Python 3

File details

Details for the file apifreaks-1.0.0.tar.gz.

File metadata

  • Download URL: apifreaks-1.0.0.tar.gz
  • Upload date:
  • Size: 267.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.10.12 Linux/6.8.0-87-generic

File hashes

Hashes for apifreaks-1.0.0.tar.gz
Algorithm Hash digest
SHA256 d08e00d6f595441ebbe664edcdc6d3be3e313f1c7e79c5a05c5d7f5188b5ecfd
MD5 08ea29f70031615ebca380f0ca084216
BLAKE2b-256 401462b37e185212f46f3017a0120a889d459d11cbc9a462e1bf9667822bc145

See more details on using hashes here.

File details

Details for the file apifreaks-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: apifreaks-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 624.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.10.12 Linux/6.8.0-87-generic

File hashes

Hashes for apifreaks-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e32ac773151762496dbfbfa870976202271c077bbc1b40c2831fdbb8dcdc07d1
MD5 b1d54581207d38d04557f75d6f24cb52
BLAKE2b-256 8901e8e72dda7b9563d015de8e387a30ea5987909d30b05e896d0f311fe92f3d

See more details on using hashes here.

Supported by

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