Skip to content

resend.ApiKey issue - Documentation discrepancy #134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
rexwithluv opened this issue Feb 14, 2025 · 3 comments
Open

resend.ApiKey issue - Documentation discrepancy #134

rexwithluv opened this issue Feb 14, 2025 · 3 comments

Comments

@rexwithluv
Copy link

In the official documentation, resend.api_key is used to set the API key and send emails. However, after testing, I realized that it does not load the API key correctly. Instead, it needs to use resend.ApiKey to properly authenticate and send the email.

def send_email(email: str, text: str, api_key: str) -> None:
    resend.ApiKey = os.getenv(api_key)

    email = resend.Emails.send(
        {
            "from": "onboarding@resend.dev",
            "to": [email],
            "subject": "hey :)",
            "text": text,
        }
    )
@drish
Copy link
Collaborator

drish commented Feb 14, 2025

hey @rexwithluv I believe you have the api key set as an env var ? check your os.environ["RESEND_API_KEY"], the SDK will raise resend.exceptions.ValidationError: API key is invalid when the api key is not properly set.

it seems that in your example code resend.ApiKey = os.getenv(api_key) you're setting the Resend ApiKey type, not the actual api key used to make http requests.

this code below works fine.

import resend

resend.api_key = "re_....."

params: resend.Emails.SendParams = {
    "from": "onboarding@resend.dev",
    "to": ["delivered@resend.dev"],
    "subject": "hi",
    "html": "<strong>hello, world!</strong>",
}


email: resend.Email = resend.Emails.send(params)

lmk if you still runs into any issues 👍

@rexwithluv
Copy link
Author

I managed to fix it, but I don't fully understand the exact cause of this. When I call it using os.environ(), it loads correctly, but it doesn't work when I use os.getenv().

Example:

# This works correctly
resend.api_key = os.environ(api_key)

# This returns an exception
resend.api_key = os.getenv(api_key)

@drish
Copy link
Collaborator

drish commented Feb 19, 2025

I managed to fix it, but I don't fully understand the exact cause of this. When I call it using os.environ(), it loads correctly, but it doesn't work when I use os.getenv().

This works correctly
resend.api_key = os.environ(api_key)

This returns an exception
resend.api_key = os.getenv(api_key)

hey @rexwithluv if your RESEND_API_KEY is already set in your environment variables you won't need to call resend.api_key= it should be already loaded by the SDK automatically.

you only need to set the resend.api_key using the setter ie: resend.api_key="something" if you're not using the RESEND_API_KEY env var.

if you share your full code I can help you find the root cause of why it wasn't working before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants