Authentication
This document covers the authentication methods for Sling API specifications. For the basic structure, see Structure.
Sling supports several authentication methods, configured under the authentication key.
⚠️ Important: It's best practice to use environment variables or secrets for sensitive values instead of hardcoding them.
Managing Secrets and Environment Variables
Sling provides flexible ways to manage sensitive authentication data through environment variables and the env.yaml file. Here's how to configure them:
Using the env.yaml File
The primary method for managing secrets is through the env.yaml file located at ~/.sling/env.yaml. When defining API connections, you can specify secrets that will be available to your API specs.
# ~/.sling/env.yaml
connections:
my_api:
type: api
spec: file:///path/to/my_api.spec.yaml
secrets:
api_key: "your-secret-api-key"
client_id: "your-oauth-client-id"
client_secret: "your-oauth-client-secret"
username: "your-username"
password: "your-password"
github_api:
type: api
spec: github
secrets:
token: "ghp_xxxxxxxxxxxxxxxxxxxx"
stripe_api:
type: api
spec: stripe
secrets:
api_key: "sk_test_xxxxxxxxxxxxxxxxxxxx"You'll be able to access the secrets values via the {secrets.var_name} expression.
Using Environment Variables
You can also provide secrets via environment variables. In your API spec, use the {env.VARIABLE_NAME} syntax:
Authentication Methods
Now that you understand how to manage secrets, here are the different authentication methods you can configure:
No Authentication
Static Header Authentication
For APIs that require headers for authentication (e.g., API keys, bearer tokens):
This is ideal for:
API keys passed in headers (e.g.,
X-API-Key: your-key)Bearer tokens (e.g.,
Authorization: Bearer your-token)Custom authentication headers
Multiple authentication headers
Examples:
Shorthand Syntax:
When only headers are provided without any other authentication configuration, the type defaults to static:
📝 Note: For dynamic token retrieval (e.g., login flow), use the
sequenceauthentication type instead.
Basic Auth
OAuth2 Authentication
For OAuth2 flows, use the oauth2 type. Sling supports three OAuth2 flows:
client_credentials
Server-to-server, machine-to-machine
No
authorization_code
User-facing apps, browser-based auth
Yes
device_code
CLI tools, headless environments
Yes (on another device)
Full Property Reference:
Token Persistence: Sling automatically stores OAuth tokens in
~/.sling/api/tokens/{connection_name}.jsonand refreshes them when they expire. You don't need to manage token refresh manually.
PKCE Support: For public clients (when
client_secretis empty), Sling automatically enables PKCE (Proof Key for Code Exchange) for added security.
Client Credentials Flow (Server-to-Server):
The most common flow for automated data pipelines. No user interaction required.
Authorization Code Flow (Browser-Based):
For user-interactive authentication. Sling opens a browser for authorization and handles the callback automatically.
Device Code Flow (Headless/CLI):
For environments without a browser. Sling displays a URL and code for the user to enter on another device.
AWS Signature V4 Authentication
For AWS services that require Signature V4 authentication (e.g., S3, AppSync, API Gateway):
📝 Note: For AWS authentication, you can use either explicit credentials (
aws_access_key_id,aws_secret_access_key) or AWS profiles (aws_profile). The AWS SDK credential chain is also supported.
HMAC Authentication
For APIs that require HMAC (Hash-based Message Authentication Code) request signing, such as cryptocurrency exchanges (Kraken, Binance) or custom enterprise APIs:
How HMAC Works:
A signing string is constructed from request components (method, path, timestamp, body hash, etc.)
The string is signed using HMAC-SHA256 or HMAC-SHA512 with your secret key
The signature and related headers are automatically added to each request
Available Variables for Signing:
http_method- HTTP method (GET, POST, etc.)http_path- Request path with query parametershttp_query- Canonical query string (sorted alphabetically by key)http_headers- Canonical headers (lowercase, sorted, newline-separated)http_body_raw- Raw request body as stringhttp_body_md5- MD5 hash of request body (hex-encoded)http_body_sha1- SHA1 hash of request body (hex-encoded)http_body_sha256- SHA256 hash of request body (hex-encoded)http_body_sha512- SHA512 hash of request body (hex-encoded)unix_time- Unix timestamp in secondsunix_time_ms- Unix timestamp in millisecondsdate_iso- ISO 8601 formatted date (e.g., "2023-10-31T15:30:32Z")date_rfc1123- RFC 1123 formatted date (e.g., "Tue, 31 Oct 2023 15:30:32 GMT")nonce- Random hex string (ifnonce_lengthis set)signature- Computed HMAC signature (hex-encoded, only available inrequest_headers)
Example: Kraken-style Authentication
📝 Note: The
signing_stringandrequest_headerstemplates support all Sling template variables including{secrets.*},{env.*}, and{state.*}.
Sequence Authentication (Custom Workflows)
For APIs requiring a custom authentication sequence (e.g., multi-step login), use the sequence type. This allows defining a series of API calls to obtain authentication tokens or session data.
This performs a login request and extracts the token into state.token for use in subsequent requests. See Requests & Responses for more on sequences.
Endpoint-Level Authentication
By default, all endpoints use the authentication configured at the spec level. However, individual endpoints can override or disable authentication.
Overriding Authentication
An endpoint can specify its own authentication that overrides the spec-level configuration:
Disabling Authentication
Some endpoints (like health checks or public data) may not require authentication. Set authentication: null to disable it:
Authentication Expiry
The expires property can be used with any authentication type to force re-authentication after a specified number of seconds. This is useful when tokens or sessions have a fixed lifetime.
When authentication expires, Sling automatically re-authenticates before the next request. This happens transparently without interrupting data extraction.
Authentication Method Comparison
static
API keys, bearer tokens
N/A
No
basic
Username/password APIs
N/A
No
oauth2
Modern APIs, delegated auth
Yes
Depends on flow
aws-sigv4
AWS services
Yes
No
hmac
Crypto exchanges, signed requests
N/A
No
sequence
Custom auth workflows
No (use expires)
No
Last updated
Was this helpful?