Skip to content

Interacting With the API

seria edited this page May 17, 2024 · 10 revisions

Genshin Impact

To interact with the Enka Network Genshin Impact API, use the GenshinClient.

import enka

async with enka.GenshinClient() as client:
    await client.fetch_showcase(901211014)

Alternatively, call the start and close methods manually.

import enka

client = enka.GenshinClient()
await client.start()
await client.fetch_showcase(901211014)
await client.stop()

Important

When using the client, you must use either the async with syntax or call start and stop manually; otherwise, the client won't work and RuntimeError will be raised.

Namecards and icons

Available after v1.2.0

Character icons are now Icon objects, you can access all types of icons from it:

The same goes for ShowcaseCharacter.costume_icon and Player.profile_picture_icon.

Namecards are now Namecard objects:

Stats

Available after v1.4.0

Stats refer to character, weapon, and artifact stats.
Internally, stats for characters are FightProp classes, while the others are Stat classes; they can be accessed in the same way, but their types are different (FightPropType and StatType).
For your convenience, there are stat.is_percentage and stat.formatted_value to use, for exmple:

  • If stat.type is StatType.FIGHT_PROP_CUR_ATTACK
    • stat.is_percentage = False
    • stat.formatted_value = '2300'
  • If stat.type is StatType.FIGHT_PROP_CRITICAL
    • stat.is_percentage = True
    • stat.formatted_value = '23.1%'

Constellations

Available after v1.7.0

After v1.7.0, all constellations that belong to a character will appear in Character.constellations, the Constellation.unlocked attribute will indicate whether the constellation is unlocked. Also, there is a new attribute Character.constellations_unlocked that indicates how many constellations the character has unlocked.

Honkai: Star Rail

Similarly, to interact with the Enka Network HSR API, use the HSRClient.

import enka

async with enka.HSRClient() as client:
    await client.fetch_showcase(809162009)

Same as GenshinClient, you can also call the start and close methods manually.

import enka

client = enka.HSRClient()
await client.start()
await client.fetch_showcase(809162009)
await client.stop()

Fetching Character Builds

Available after v2.1.0

For information on what character builds are, please read the Enka Network API docs.
client.fetch_builds returns a dictionary of character ID to list of builds that belong to the character, builds is a list of Build models.

import enka

async with enka.GenshinClient() as client:
    showcase = await client.fetch_showcase(618285856)
    builds = await client.fetch_builds(showcase.owner)
    
    for character_id, build in builds.items():
        print(character_id)
        print(build.name, build.character.name)

# Same goes for HSRClient

Fetching and Parsing raw Data

Available after v2.1.1

You can let the API wrapper return the raw data from the API, and later on parse it.

import enka

async with enka.GenshinClient() as client:
    raw = await client.fetch_showcase(901211014, raw=True)
    parsed = client.parse_showcase(raw)

# Same goes for HSRClient

Examples

You can find more detailed examples in the examples folder.