Usage¶
First Steps¶
To start calling the API, you first need to instantiate a Client with your API key:
client = clash_royale.Client(api_key='your-api-key')
While this usage is simple and convenient for using in the Python console, it’s best to use it as a context manager:
with clash_royale.Client(api_key='your-api-key') as client:
...
This is the recommended way to use it by httpx, which is the library we use under the hood.
From there, you can search for clans:
client.clans.search('Legend')
<PaginatedList [
<ClanSearchResult: Legend>,
<ClanSearchResult: Legend Squad>,
<ClanSearchResult: The Legend>,
<ClanSearchResult: Legends>,
<ClanSearchResult: Legend Empire>,
'...']>
The above returned a lot of clans, wrapped in a PaginatedList, which is a list-like object (see the dedicated page about pagination for more details).
You can also get information about a specific player by their tag:
client.players.get('#9G9JL8QU')
<Player: PlayerName>
Main Concepts¶
As we have just seen above, the entry point is the Client class, which gives access to various resource endpoints. The methods are attempting to map to the REST API endpoints from Clash Royale.
You may have noticed from the above examples, but depending on the endpoint that is being called, the methods will return various types of resources. All the resources are listed in the resources reference page.
More Examples¶
Getting Fields About a Resource¶
When you get a resource, you have access to all the fields that are in the REST API response. For example, all the fields presented in the documentation for the player object are accessible as attributes on the Player resource:
>>> player = client.players.get('#9G9JL8QU')
>>> player.name
'PlayerName'
>>> player.trophies
5432
>>> player.level
14
>>> player.wins
1234
Working with Leaderboards¶
You can fetch leaderboard data with pagination control:
# Get available leaderboards
>>> leaderboards = client.leaderboards.list()
>>> leaderboards[0]
<Leaderboard: Players>
# Get top players from a leaderboard
>>> players = client.leaderboards.get(leaderboard_id=57000000, limit=10)
... for player in players:
... print(f"{player.rank}. {player.name} - {player.trophies}")
Searching with Filters¶
When searching for clans or tournaments, you can use filters:
# Search clans with filters
clans = client.clans.search(
name='Legend',
min_members=40,
location_id=57000249, # United States
limit=20
)
for clan in clans:
print(f"{clan.name} - {clan.members} members")
Getting the Raw Data¶
At some point, you might want to get the resources exported as Python dictionaries to store them somewhere else or transform them further.
Each resource has a model_dump() method to export its content as a dictionary:
>>> player = client.players.get('#9G9JL8QU')
>>> player.model_dump()
{'tag': '#9G9JL8QU',
'name': 'PlayerName',
'level': 14,
'trophies': 5432,
'best_trophies': 6123,
'wins': 1234,
'losses': 987,
...}
You can also use model_dump_json() to get a JSON string directly:
>>> player.model_dump_json()
'{"tag":"#9G9JL8QU","name":"PlayerName",...}'
Authentication¶
The Clash Royale API requires an API key for all requests. You can get an API key by:
Creating an account at https://developer.clashroyale.com
Creating an API key with your IP address whitelisted
Once you have your API key, pass it to the Client:
client = clash_royale.Client(api_key='your-api-key')
Important: Keep your API key secret! Don’t commit it to version control. Consider using environment variables:
import os
client = clash_royale.Client(api_key=os.environ['CLASH_ROYALE_API_KEY'])
Using a proxy¶
If your server does not have a static IP address, you can use a proxy server.
For exemple you can use the proxy solution provided by the RoyaleAPI team by setting the proxy parameter to https://proxy.royaleapi.dev.
>>> client = clash_royale.Client(
api_key="your_api_key_here",
proxy="https://proxy.royaleapi.dev",
)
For more detail about RoyaleAPI proxy usage, check their documentation.
If you want to host your own proxy server, there is a nuxt web app you can use available at https://github.com/AndreVarandas/royale-proxy-api. Made by @AndreVarandas
Error Handling¶
The client will raise specific exceptions for different error scenarios:
import clash_royale
try:
player = client.players.get('#INVALID')
except clash_royale.ClashRoyaleNotFoundError:
print("Player not found")
except clash_royale.UnauthorizedError:
print("Invalid API key")
except clash_royale.RateLimitError:
print("Rate limit exceeded")
except clash_royale.ClashRoyaleHTTPError as e:
print(f"API error: {e}")