# Unable to run script despite migrating to new CDS API

**URL:** <https://forum.ecmwf.int/t/unable-to-run-script-despite-migrating-to-new-cds-api/5608>\
**Category:** Access and Login\
**Tags:** api\
**Created:** [16 September 2024 18:48 UTC](https://forum.ecmwf.int/t/unable-to-run-script-despite-migrating-to-new-cds-api/5608 "2024-09-16T18:48:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sohum\_Chatterjee](https://forum.ecmwf.int/user_avatar/forum.ecmwf.int/sohum_chatterjee/32/499_2.png) [@Sohum\_Chatterjee](https://forum.ecmwf.int/u/Sohum_Chatterjee)\
**Post date:** [16 September 2024 18:48 UTC](https://forum.ecmwf.int/t/unable-to-run-script-despite-migrating-to-new-cds-api/5608/1 "2024-09-16T18:48:02Z")

</div>

I upgraded the .cdsapirc file with the new url and key, and also upgraded my cdsapi package to 0.7.3, but when I run my script, I keep getting this error:

Traceback (most recent call last):  
File “c:\Users\USER\OneDrive\Documents\Programs\Code for Storms\Code-for-storms\TestingStreamlines.py”, line 81, in   
main()  
File “c:\Users\USER\OneDrive\Documents\Programs\Code for Storms\Code-for-storms\TestingStreamlines.py”, line 76, in main  
download\_data()  
File “c:\Users\USER\OneDrive\Documents\Programs\Code for Storms\Code-for-storms\TestingStreamlines.py”, line 26, in download\_data  
client = cdsapi.Client()  
File “C:\ProgramData\Miniconda3\lib\site-packages\cdsapi\api.py”, line 337, in **init**  
assert len(self.session.auth) == 2, (  
AssertionError: The cdsapi key provided is not the correct format, please ensure it conforms to:  
:

It seems that the api still thinks that the .cdsapirc file should be similar to the old version, so I cannot use it right now. Please suggest help!

Here’s the script I am using:

import cfgrib  
import numpy as np  
import matplotlib.pyplot as plt  
import matplotlib.colors as mcolors  
import cartopy.crs as ccrs  
import cartopy.feature as cfeature

def download\_data():  
import cdsapi

```
dataset = "reanalysis-era5-pressure-levels"
request = {
    'product_type': ['reanalysis'],
    'variable': ['u_component_of_wind', 'v_component_of_wind'],
    'year': ['2000'],
    'month': ['02'],
    'day': ['06'],
    'time': ['00:00'],
    'pressure_level': ['200'],
    'data_format': 'grib',
    'download_format': 'unarchived',
    'area': [0, 90, -20, 120]
}

client = cdsapi.Client()
client.retrieve(dataset, request).download()

```

def read\_grib\_data(grib\_file):  
ds = cfgrib.open\_dataset(grib\_file)  
u850\_data = ds[‘u’].values  
v850\_data = ds[‘v’].values

```
lats = ds['latitude'].values
lons = ds['longitude'].values

return lons, lats, u850_data, v850_data

```

def plot\_streamlines(lons, lats, u850\_data, v850\_data):

```
wind_speed_m_s = np.sqrt(u850_data **2 + v850_data** 2)

wind_speed_knots = wind_speed_m_s * 1.94384

fig = plt.figure(figsize=(12, 8))
ax = plt.axes(projection=ccrs.PlateCarree())

ax.set_extent([123, 133, 18, 28], crs=ccrs.PlateCarree())

ax.add_feature(cfeature.COASTLINE)
ax.add_feature(cfeature.BORDERS)
ax.add_feature(cfeature.LAND, edgecolor='black')
ax.add_feature(cfeature.OCEAN, edgecolor='black')

ax.gridlines(draw_labels=True, linewidth=0.5, color='gray', alpha=0.5, linestyle='--')

strm = ax.streamplot(lons, lats, u850_data, v850_data, transform=ccrs.PlateCarree(), linewidth=1, density=2,
                     color=wind_speed_knots, cmap='gist_ncar', arrowstyle='->', arrowsize=1.5)

cbar = plt.colorbar(strm.lines, ax=ax, orientation='horizontal', pad=0.05, aspect=50, shrink=0.75)
cbar.set_label('Wind Speed (knots)')

plt.title('Streamlines of Winds at 200 hPa\n 12 PM UTC on October 4th, 2013')
plt.xlabel('Longitude')
plt.ylabel('Latitude')

plt.show()

```

def main():  
download\_data()  
lons, lats, u850\_data, v850\_data = read\_grib\_data(‘download.grib’)  
plot\_streamlines(lons, lats, u850\_data, v850\_data)

if **name** == “ **main** ”:  
main()

---

<div class="post-metadata">

**Author:** ![Laurens\_Stoop](https://forum.ecmwf.int/user_avatar/forum.ecmwf.int/laurens_stoop/32/480_2.png) [@Laurens\_Stoop](https://forum.ecmwf.int/u/Laurens_Stoop)\
**Post date:** [7 October 2024 12:42 UTC](https://forum.ecmwf.int/t/unable-to-run-script-despite-migrating-to-new-cds-api/5608/2 "2024-10-07T12:42:22Z")

</div>

**Suggestion 1:**

- Update the cdsapi package!

**Suggestion 2:**  
Provide the key in an alternative way:

> [@Sohum\_Chatterjee](#):
>
> The cdsapi key provided is not the correct format, please ensure it conforms to:

This indicates that the key is incorrect. You could try the following within your code (not neat & don’t commit to git, but quick fix):

```auto
import os

# Set the API credentials as environment variables
os.environ['CDSAPI_URL'] = 'https://cds.climate.copernicus.eu/api/v2'
os.environ['CDSAPI_KEY'] = 'your_api_key_here'

```

See also this post on other versions of this suggestion: [https://prod.ecmwf-forum-prod.compute.cci2.ecmwf.int/t/is-there-elsewhere-to-put-the-cdsapirc-file-running-it-on-databricks/956](https://prod.ecmwf-forum-prod.compute.cci2.ecmwf.int/t/is-there-elsewhere-to-put-the-cdsapirc-file-running-it-on-databricks/956)

---

<div class="post-metadata">

**Author:** ![Imanol\_Uriarte\_Latorre](https://forum.ecmwf.int/letter_avatar_proxy/v4/letter/i/898d66/32.png) [@Imanol\_Uriarte\_Latorre](https://forum.ecmwf.int/u/Imanol_Uriarte_Latorre)\
**Post date:** [22 October 2024 07:42 UTC](https://forum.ecmwf.int/t/unable-to-run-script-despite-migrating-to-new-cds-api/5608/3 "2024-10-22T07:42:32Z")

</div>

Did the API change again, from `https://cds-beta.climate.copernicus.eu/api ` to the one you posted?
