Skip to content

HTTP Module Reference

The http module provides functions for making HTTP requests, creating endpoints for feedback workflows, and handling authentication.

Create a bearer token authentication interceptor:

auth = http.bearer_auth(
creds = credentials.static_secret("token_name", "TOKEN_ENV_VAR"),
)
ParameterTypeDefaultDescription
credsCredentialIssuerrequiredThe token credential
# Bearer token authentication for API calls
http.bearer_auth(
creds = credentials.static_secret("api_token", "API_TOKEN"),
)

Create a username/password authentication interceptor:

auth = http.username_password_auth(
creds = credentials.username_password(
credentials.static_value("username"),
credentials.static_secret("password", "PASSWORD_ENV_VAR"),
),
)
ParameterTypeDefaultDescription
credsUsernamePasswordIssuerrequiredThe username/password credentials

Create an HTTP endpoint for making API requests in feedback workflows:

endpoint = http.endpoint(
hosts = ["api.example.com"],
)
ParameterTypeDefaultDescription
hostslist[]List of allowed hosts for HTTP traffic
hoststring""DEPRECATED. Use hosts instead
checkerCheckerNoneChecker to validate calls made by endpoint
issuersdict{}Dictionary of credential issuers by host name
http.endpoint(
hosts = [
http.host(
host = "api.github.com",
auth = http.bearer_auth(
creds = credentials.static_secret("github_token", "GITHUB_TOKEN"),
),
),
http.host(
host = "api.internal.com",
auth = http.username_password_auth(
creds = credentials.username_password(
credentials.static_value("service_account"),
credentials.static_secret("password", "SERVICE_PASSWORD"),
),
),
),
],
)

Create an HTTP trigger for feedback workflows:

trigger = http.trigger(
hosts = ["webhooks.example.com"],
)
ParameterTypeDefaultDescription
hostslist[]List of allowed hosts for HTTP traffic
checkerCheckerNoneChecker to validate calls made by endpoint
issuersdict{}Dictionary of credential issuers

Wrap a host with optional authentication credentials:

http.host(
host = "api.example.com",
auth = http.bearer_auth(...),
)
ParameterTypeDefaultDescription
hoststringN/AThe host to allow traffic to
authAuthInterceptorNoneAuthentication interceptor to use

Create a JSON HTTP request body:

body = http.json({
"key": "value",
"nested": {
"data": [1, 2, 3]
}
})
ParameterTypeDefaultDescription
bodyobject{}Object to serialize JSON

Create a URL-encoded form HTTP body:

body = http.urlencoded_form({
"field1": "value1",
"field2": "value2",
})
ParameterTypeDefaultDescription
bodydict{}Form fields as key-value dict

Create a multipart form HTTP body for file uploads:

body = http.multipart_form(
parts = [
http.multipart_form_text("field_name", "text value"),
http.multipart_form_file("file_field", ctx.path("file.txt")),
],
)
ParameterTypeDefaultDescription
partslist[]List of form parts to upload

Create a text part for multipart form:

http.multipart_form_text("field_name", "text value")
ParameterTypeDefaultDescription
namestringN/AName of the form field
textstringN/AText value for the field

Create a file part for multipart form:

http.multipart_form_file(
"file_field",
ctx.path("path/to/file.txt"),
content_type = "text/plain",
filename = "custom_name.txt",
)
ParameterTypeDefaultDescription
namestringN/AName of the form field
pathCheckoutPathN/APath to the file in checkout
content_typestring"application/octet-stream"MIME content type for the file
filenamestringNoneCustom filename (defaults path)

URL-encode a string:

encoded = http.url_encode("hello world")
# Returns: "hello%20world"
ParameterTypeDefaultDescription
inputstringN/AString to URL-encode

Post status updates to an external API:

def _notify_status(ctx):
response = ctx.destination.post(
url = "https://api.example.com/status",
headers = {"Content-Type": "application/json"},
body = http.json({
"repo": ctx.params["repo"],
"status": "synced",
"ref": ctx.ref,
}),
)
if response.status_code != 200:
return ctx.error("Failed to post status: " + str(response.status_code))
return ctx.success()
core.feedback(
name = "notify",
origin = git.github_trigger(
url = "https://github.com/org/repo",
events = ["push"],
),
destination = http.endpoint(
hosts = [
http.host(
host = "api.example.com",
auth = http.bearer_auth(
creds = credentials.static_secret("api_token", "API_TOKEN"),
),
),
],
),
actions = [
core.action(
impl = _notify_status,
params = {"repo": "org/repo"},
),
],
)

Upload files to an API:

def _upload_artifact(ctx):
response = ctx.destination.post(
url = "https://artifacts.example.com/upload",
body = http.multipart_form(
parts = [
http.multipart_form_text("version", ctx.ref),
http.multipart_form_file(
"artifact",
ctx.path("dist/package.tar.gz"),
content_type = "application/gzip",
),
],
),
)
return ctx.success() if response.status_code == 200 else ctx.error("Upload failed")

HTTP authentication can be used with remotefiles.origin:

core.workflow(
name = "sync_private",
origin = remotefiles.origin(
archive_source = "https://private.example.com/releases/${VERSION}.tar.gz",
unpack_method = "TAR_GZ",
auth = http.bearer_auth(
creds = credentials.static_secret("download_token", "DOWNLOAD_TOKEN"),
),
),
destination = git.destination(...),
authoring = authoring.overwrite("Bot <bot@example.com>"),
)