from typing import Union
from datetime import datetime


def datetime_to_iso_8601(obj: Union[datetime, str, int]) -> str:
    """
    Convert a datetime to ISO 8601. For use when serializing the credentials dict.

    :param obj: a datetime object
    :return: the ISO 8601 representation of the datetime
    """
    if isinstance(obj, datetime):
        return obj.isoformat()
    raise TypeError(f"{type(obj)} is not serializable")