Skip to content

Identifier

Within OpenAlex, IDs consist of two core components: a consistent base, https://openalex.org/, and a unique resource identifier called the Key, such as A000000000. Consequently, a complete ID is structured as https://openalex.org/A000000000.

While querying the API using the full ID is an option, at pub analyzer, we prefer utilizing only the key. This choice enhances the clarity of logs and proves especially helpful during debugging.

Hence, the subsequent functions have been devised. They accept entity models as input and yield the corresponding keys from their IDs.

Info

For further details about the OpenAlex IDs, consult the documentation.

How to recognize the type of entity?

You can deduce the entity's type from the ID itself. This is possible because all keys commence with a letter that corresponds to the entity's type: W(ork), A(uthor), S(ource), I(nstitution), C(oncept), P(ublisher), or F(under). Quite convenient, isn't it?

Functions to extract OpenAlex IDs from Models.

get_author_id

def get_author_id(
    author: Author | AuthorResult | DehydratedAuthor,
) -> AuthorOpenAlexKey

Extract OpenAlex ID from author Model.

Parameters:

Name Type Description Default
author Author | AuthorResult | DehydratedAuthor

Author model instance.

required

Returns:

Type Description
AuthorOpenAlexKey

Author OpenAlex ID.

Example
from pub_analyzer.internal.identifier import get_author_id
from pub_analyzer.models.author import DehydratedAuthor

author = DehydratedAuthor(id="https://openalex.org/A000000000")
print(get_author_id(author))
# 'A000000000'

get_institution_id

def get_institution_id(
    institution: Institution
    | InstitutionResult
    | DehydratedInstitution,
) -> InstitutionOpenAlexKey

Extract OpenAlex ID from institution Model.

Parameters:

Name Type Description Default
institution Institution | InstitutionResult | DehydratedInstitution

Institution model instance.

required

Returns:

Type Description
InstitutionOpenAlexKey

Institution OpenAlex ID.

Example
from pub_analyzer.internal.identifier import get_institution_id
from pub_analyzer.models.institution import DehydratedInstitution

institution = DehydratedInstitution(id="https://openalex.org/I000000000", **kwargs)
print(get_institution_id(institution))
# 'I000000000'

get_source_id

def get_source_id(source: DehydratedSource | Source) -> str

Extract OpenAlex ID from Source Model.

Parameters:

Name Type Description Default
source DehydratedSource | Source

Source model instance.

required

Returns:

Type Description
str

Source OpenAlex ID.

Example
from pub_analyzer.internal.identifier import get_source_id
from pub_analyzer.models.source import Source

source = Source(id="https://openalex.org/S000000000", **kwargs)
print(get_source_id(source))
# 'S000000000'

get_work_id

def get_work_id(work: Work) -> str

Extract OpenAlex ID from Work Model.

Parameters:

Name Type Description Default
work Work

Work model instance.

required

Returns:

Type Description
str

Work OpenAlex ID.

Example
from pub_analyzer.internal.identifier import get_work_id
from pub_analyzer.models.work import Work

work = Work(id="https://openalex.org/W000000000", **kwargs)
print(get_work_id(work))
# 'W000000000'