skip to Main Content

I have an API Key with full access and my account includes the "additional email activity history" (additional payment)

When using a simple PHP request I am getting 400 error and "authorization required"

400 Array ( [0] => HTTP/1.1 400 Bad Request [1] => Server: nginx [2] => Date: Thu, 16 Mar 2023 17:20:53 GMT [3] => Content-Type: application/json [4] => Content-Length: 62 [5] => Connection: keep-alive [6] => [7] => ) {"errors":[{"field":null,"message":"authorization required"}]}

Here is my code copied from their documentation

require_once 'sendgrid-php.php'; 

    $apiKey = getenv('SG.xxxxxxx');
    $sg = new SendGrid($apiKey);
    
    $query_params = json_decode('{
        "query": "from_email="[email protected]"",
        "limit": 10
    }');
    
    
    try {
        $response = $sg->client->messages()->get(null, $query_params);
        print $response->statusCode() . "n";
        print_r($response->headers());
        print $response->body() . "n";
    } catch (Exception $ex) {
        echo 'Caught exception: '.  $ex->getMessage();
    }
    

Any help will be well appreciated

2

Answers


  1. Chosen as BEST ANSWER

    Just changed the:

    $apiKey = getenv('SG.xxxxxxx');
    

    to:

    $apiKey = 'SG.xxxxxxx';
    

  2. For Windows:
    Put .env file in the root folder of the project with this line in it:

    SENDGRID_API_KEY=your_key
    

    Set the same line as your user environment variable in Windows.
    Restart the machine.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search