# Upload Error when api.upload\_file

**URL:** <https://discuss.huggingface.co/t/upload-error-when-api-upload-file/30359>\
**Category:** Spaces\
**Created:** [January 24, 2023, 10:45pm UTC](https://discuss.huggingface.co/t/upload-error-when-api-upload-file/30359 "2023-01-24T22:45:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![emmajoanne](https://avatars.discourse-cdn.com/v4/letter/e/ce73a5/32.png) [@emmajoanne](https://discuss.huggingface.co/u/emmajoanne)\
**Post date:** [January 24, 2023, 10:45pm UTC](https://discuss.huggingface.co/t/upload-error-when-api-upload-file/30359/1 "2023-01-24T22:45:10Z")

</div>

Hello,

I am new to the HuggingFace space API and I am currently attempting to upload a file to my existing model repository using the upload\_file API. My upload part is as below:

```auto
try:
    api.upload_file(
        path_or_fileobj="/home/user/app/tmp/test.zip",
        path_in_repo="test.zip",
        repo_id="emmajoanne/models",
        repo_type="model",
    )
except:
    return "Upload error!"

```

It always falls into the except but doesn’t provide any more details.

From the official document ([Upload files to the Hub](https://huggingface.co/docs/huggingface_hub/v0.11.0/en/how-to-upstream#upload-a-file)) I don’t see any error handling information or what exceptions it throws.

Appreciate.

---

<div class="post-metadata">

**Author:** ![radames](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/radames/32/28246_2.png) [@radames](https://discuss.huggingface.co/u/radames)\
**Post date:** [January 25, 2023, 3:45am UTC](https://discuss.huggingface.co/t/upload-error-when-api-upload-file/30359/2 "2023-01-25T03:45:46Z")

</div>

hi @emmajoanne , what’s the error message? do you have the write access token? have you login before attempting to write on your repo?

> **[Quickstart](https://huggingface.co/docs/huggingface_hub/main/en/quick-start#login)**
>
> We’re on a journey to advance and democratize artificial intelligence through open source and open science.

> **[Hugging Face – The AI community building the future.](https://huggingface.co/login?next=%2Fsettings%2Ftokens)**
>
> We’re on a journey to advance and democratize artificial intelligence through open source and open science.

---

<div class="post-metadata">

**Author:** ![emmajoanne](https://avatars.discourse-cdn.com/v4/letter/e/ce73a5/32.png) [@emmajoanne](https://discuss.huggingface.co/u/emmajoanne)\
**Post date:** [January 25, 2023, 5:17am UTC](https://discuss.huggingface.co/t/upload-error-when-api-upload-file/30359/3 "2023-01-25T05:17:49Z")

</div>

Thank you for your response.  
I was able to locate the error message by printing out the `BaseException as e`. The problem is related to unauthorized access. From this error, I was able to determine that the root cause is an issue with the way I initialized the api lib.

So I updated my code from:

```auto
api = HfApi(token)

```

to :

```auto
api = HfApi(token=token)

```

Then, problem solved.
