103 |
- |
1 |
<?php
|
|
|
2 |
// If you are using Composer
|
|
|
3 |
require 'vendor/autoload.php';
|
|
|
4 |
|
|
|
5 |
|
|
|
6 |
$apiKey = getenv('SENDGRID_API_KEY');
|
|
|
7 |
$sg = new \SendGrid($apiKey);
|
|
|
8 |
|
|
|
9 |
////////////////////////////////////////////////////
|
|
|
10 |
// Create API keys #
|
|
|
11 |
// POST /api_keys #
|
|
|
12 |
|
|
|
13 |
$request_body = json_decode('{
|
|
|
14 |
"name": "My API Key",
|
|
|
15 |
"sample": "data",
|
|
|
16 |
"scopes": [
|
|
|
17 |
"mail.send",
|
|
|
18 |
"alerts.create",
|
|
|
19 |
"alerts.read"
|
|
|
20 |
]
|
|
|
21 |
}');
|
|
|
22 |
$response = $sg->client->api_keys()->post($request_body);
|
|
|
23 |
echo $response->statusCode();
|
|
|
24 |
echo $response->body();
|
|
|
25 |
echo $response->headers();
|
|
|
26 |
|
|
|
27 |
////////////////////////////////////////////////////
|
|
|
28 |
// Retrieve all API Keys belonging to the authenticated user #
|
|
|
29 |
// GET /api_keys #
|
|
|
30 |
|
|
|
31 |
$query_params = json_decode('{"limit": 1}');
|
|
|
32 |
$response = $sg->client->api_keys()->get(null, $query_params);
|
|
|
33 |
echo $response->statusCode();
|
|
|
34 |
echo $response->body();
|
|
|
35 |
echo $response->headers();
|
|
|
36 |
|
|
|
37 |
////////////////////////////////////////////////////
|
|
|
38 |
// Update the name & scopes of an API Key #
|
|
|
39 |
// PUT /api_keys/{api_key_id} #
|
|
|
40 |
|
|
|
41 |
$request_body = json_decode('{
|
|
|
42 |
"name": "A New Hope",
|
|
|
43 |
"scopes": [
|
|
|
44 |
"user.profile.read",
|
|
|
45 |
"user.profile.update"
|
|
|
46 |
]
|
|
|
47 |
}');
|
|
|
48 |
$api_key_id = "test_url_param";
|
|
|
49 |
$response = $sg->client->api_keys()->_($api_key_id)->put($request_body);
|
|
|
50 |
echo $response->statusCode();
|
|
|
51 |
echo $response->body();
|
|
|
52 |
echo $response->headers();
|
|
|
53 |
|
|
|
54 |
////////////////////////////////////////////////////
|
|
|
55 |
// Update API keys #
|
|
|
56 |
// PATCH /api_keys/{api_key_id} #
|
|
|
57 |
|
|
|
58 |
$request_body = json_decode('{
|
|
|
59 |
"name": "A New Hope"
|
|
|
60 |
}');
|
|
|
61 |
$api_key_id = "test_url_param";
|
|
|
62 |
$response = $sg->client->api_keys()->_($api_key_id)->patch($request_body);
|
|
|
63 |
echo $response->statusCode();
|
|
|
64 |
echo $response->body();
|
|
|
65 |
echo $response->headers();
|
|
|
66 |
|
|
|
67 |
////////////////////////////////////////////////////
|
|
|
68 |
// Retrieve an existing API Key #
|
|
|
69 |
// GET /api_keys/{api_key_id} #
|
|
|
70 |
|
|
|
71 |
$api_key_id = "test_url_param";
|
|
|
72 |
$response = $sg->client->api_keys()->_($api_key_id)->get();
|
|
|
73 |
echo $response->statusCode();
|
|
|
74 |
echo $response->body();
|
|
|
75 |
echo $response->headers();
|
|
|
76 |
|
|
|
77 |
////////////////////////////////////////////////////
|
|
|
78 |
// Delete API keys #
|
|
|
79 |
// DELETE /api_keys/{api_key_id} #
|
|
|
80 |
|
|
|
81 |
$api_key_id = "test_url_param";
|
|
|
82 |
$response = $sg->client->api_keys()->_($api_key_id)->delete();
|
|
|
83 |
echo $response->statusCode();
|
|
|
84 |
echo $response->body();
|
|
|
85 |
echo $response->headers();
|