Getting Started¶
This guide introduces you to the basics of using the gfw-api-python-client. For detailed and alternative installation instructions, please refer to the Installation section. Here is a Jupyter Notebook version for this guide.
Note: See the Datasets, Data Caveats, and Terms of Use pages in the GFW API documentation for details on GFW data, API licenses, and rate limits.
Installation¶
The gfw-api-python-client can be easily installed using pip:
pip install gfw-api-python-client
For more detailed installation instructions, including setting up a virtual environment, please see the dedicated Installation section.
Basic Usage¶
Once installed, you can import and use gfw-api-python-client in your Python codes:
import datetime
import os
import gfwapiclient as gfw
access_token = os.environ.get(
"GFW_API_ACCESS_TOKEN",
"<OR_PASTE_YOUR_GFW_API_ACCESS_TOKEN_HERE>",
)
gfw_client = gfw.Client(
access_token=access_token,
)
Important: The
gfw-api-python-clientis async-first: all API methods are asynchronous and must be executed using Python’sasync/awaitsyntax. Environments with built-in async support (e.g., Jupyter Notebook, Google Colab, and async web frameworks such as FastAPI) can call client methods directly usingawait, with no additional setup. Environments without built-in async support (e.g., plain Python scripts, Python modules, or synchronous web frameworks such as Flask and Django) must explicitly run asynchronous code using an event loop. This can be done with tools such asasyncio.run,trio.run,anyio.run, or framework-specific async integration.
Tip: Use IPython or Python 3.11+ with
python -m asyncioto rungfw-api-python-clientcode interactively, as these environments support executingasync/awaitexpressions directly in the console.
Making API Requests¶
Searching for a Vessel by MMSI¶
vessel_search_result = await gfw_client.vessels.search_vessels(
query="412331038",
)
vessel_search_ids = [
self_reported_info.id
for vessel_search_item in vessel_search_result.data()
if vessel_search_item.registry_info_total_records >= 1
for self_reported_info in vessel_search_item.self_reported_info
]
print(vessel_search_ids)
Output:
['755a48dd4-4bee-4bcf-7b5f-9baea058fc7b', '3dad49b0b-b2e0-9347-0c4c-e39fea560f9f']
Note: It is recommended to prioritize vessels that include both registry_info and self_reported_info (AIS), as this indicates a successful match between registry data and AIS information.
Getting Details of Vessels Filtered by Vessel Searched IDs¶
vessels_result = await gfw_client.vessels.get_vessels_by_ids(
ids=vessel_search_ids,
)
vessel_self_reported_infos = [
self_reported_info
for vessel_item in vessels_result.data()
for self_reported_info in vessel_item.self_reported_info
]
vessel_ids = [
self_reported_info.id for self_reported_info in vessel_self_reported_infos
]
print(vessel_ids)
Output:
['755a48dd4-4bee-4bcf-7b5f-9baea058fc7b', '3dad49b0b-b2e0-9347-0c4c-e39fea560f9f']
Getting Fishing Events for the Vessels Searched¶
events_result = await gfw_client.events.get_all_events(
datasets=["public-global-fishing-events:latest"],
start_date=start_date,
end_date=end_date,
vessels=vessel_ids,
)
events_df = events_result.df()
print(events_df.info())
Output:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 398 entries, 0 to 397
Data columns (total 14 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 start 398 non-null datetime64[ns, UTC]
1 end 398 non-null datetime64[ns, UTC]
2 id 398 non-null object
3 type 398 non-null object
4 position 398 non-null object
5 regions 398 non-null object
6 bounding_box 398 non-null object
7 distances 398 non-null object
8 vessel 398 non-null object
9 encounter 0 non-null object
10 fishing 398 non-null object
11 gap 0 non-null object
12 loitering 0 non-null object
13 port_visit 0 non-null object
dtypes: datetime64[ns, UTC](2), object(12)
memory usage: 43.7+ KB
Next Steps¶
This guide has provided you with the fundamental steps to install and use the gfw-api-python-client for making basic API requests.
To further explore the capabilities of our APIs (4Wings, Vessels, Events, Insights, Datasets, Bulk Download, References, etc.), please refer to the detailed Usage Guides and Workflow Guides. These guides delve into specific use cases and demonstrate how to effectively leverage the gfw-api-python-client for your data exploration needs.
Happy coding and data exploring!
