Appearance
Code Examples
The P1PS API can be called using any HTTP request tool/library. Below are some code examples using common languages to send an email using the P1PS API endpoint POST /api/teams/{team_id}/emails
.
Let us know in the P1PS help channel if you have a new language to be added to this list of examples.
Environment Variables
All the code examples utilize the common P1PS environment variables. We suggest that your code use them, too!
Dependencies
In the examples below we've attempted to use standard libraries for HTTP requests to highlight the P1PS API calls. You can substitute your favorite HTTP request library as necessary.
JavaScipt
javascript
// get p1ps env vars
const p1psAuthToken = process.env.P1PS_AUTH_TOKEN;
const p1psBaseUrl = process.env.P1PS_BASE_URL;
const p1psTeamId = process.env.P1PS_TEAM_ID;
// construct a request body to send an email
const requestBody = {
recipients: ["alice@email.com"],
subject: "Access to AwesomeApp.dso.mil has been granted",
template_inputs: {
date_field: "2024-01-01",
name_field: "Alice",
},
template_name: "my-first-template",
};
// call the p1ps send email endpoint
const response = await fetch(`${p1psBaseUrl}/teams/${p1psTeamId}/emails`, {
method: "POST",
headers: {
EMAIL_AUTH: p1psAuthToken,
// "Cookie" for DEV ONLY!
// see https://p1ps-docs.dso.mil/documents/development-tips for details
// "Cookie": "__Host-p1ps-staging-authservice-session-id-cookie=<your-cookie-token-here>"
},
body: JSON.stringify(requestBody),
});
console.log("status:", response.status);
console.log("json:", await response.json());
Python
python
import os
import requests
# get p1ps env vars
p1ps_auth_token = os.environ.get("P1PS_AUTH_TOKEN")
p1ps_base_url = os.environ.get("P1PS_BASE_URL")
p1ps_team_id = os.environ.get("P1PS_TEAM_ID")
# construct a request body to send an email
requestBody = {
"recipients": [
"alice@email.com"
],
"subject": "Access to AwesomeApp.dso.mil has been granted",
"template_inputs": {
"date_field": "2024-01-01",
"name_field": "Alice"
},
"template_name": "my-first-template"
}
# call the p1ps send email endpoint
response = requests.post(
f"{p1ps_base_url}/teams/{p1ps_team_id}/emails",
headers={
"EMAIL_AUTH": p1ps_auth_token,
# "Cookie" for DEV ONLY!
# see https://p1ps-docs.dso.mil/documents/development-tips for details
# "Cookie": "__Host-p1ps-staging-authservice-session-id-cookie=<your-cookie-token-here>"
},
json=requestBody)
print("status_code:", response.status_code)
print("json:", response.json())
Java
java
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpRequest.BodyPublishers;
public class Main {
public static void main(String[] args) {
// get p1ps env vars
String p1psAuthToken = System.getenv("P1PS_AUTH_TOKEN");
String p1psBaseUrl = System.getenv("P1PS_BASE_URL");
String p1psTeamId = System.getenv("P1PS_TEAM_ID");
// using a simple string body as an example. you will probably use a POJO and serialize it to JSON.
// construct a request body to send an email
String requestBody = """
{
"recipients": [
"alice@email.com"
],
"subject": "Access to AwesomeApp.dso.mil has been granted",
"template_inputs": {
"date_field": "2024-01-01",
"name_field": "Alice"
},
"template_name": "my-first-template"
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(p1psBaseUrl + "/teams/" + p1psTeamId + "/emails"))
.header("EMAIL_AUTH", p1psAuthToken)
// "Cookie" for DEV ONLY!
// see https://p1ps-docs.dso.mil/documents/development-tips for details
// .header("Cookie", "__Host-p1ps-staging-authservice-session-id-cookie=<your-cookie-token-here>")
.POST(BodyPublishers.ofString(requestBody))
.build();
try {
// call the p1ps send email endpoint
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println("statusCode:" + response.statusCode());
System.out.println("body:" + response.body());
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
}
}