# Connecting apps in Python
When you use prebuilt actions tied to apps, you don't need to write the code to authorize API requests. Just connect your account for that app and run your workflow.
But sometimes you'll need to write your own code. You can also connect apps to custom code steps, using the auth information to authorize requests to that app.
For example, you may want to send a Slack message from a step. We use Slack's OAuth integration to authorize sending messages from your workflows.
Add Slack as an app on the Python step, then connect your Slack account.
Then within the Python code step, pd.steps["slack"]["$auth"]["oauth_access_token"]
will contain your Slack account OAuth token.
With that token, you can make authenticated API calls to Slack:
from slack_sdk import WebClient
def handler(pd: "pipedream"):
# Your Slack OAuth token is available under pd.inputs
token = pd.inputs["slack"]["$auth"]["oauth_access_token"]
# Instantiate a new Slack client with your token
client = WebClient(token=token)
# Use the client to send messages to Slack channels
response = client.chat_postMessage(
channel='#general',
text='Hello from Pipedream!'
)
# Export the Slack response payload for use in future steps
pd.export("response", response.data)
# Accessing connected account data with pd.inputs[appName]["$auth"]
In our Slack example above, we created a Slack WebClient
using the Slack OAuth access token:
# Instantiate a new Slack client with your token
client = WebClient(token=token)
Where did pd.inputs["slack"]
come from? Good question. It was generated when we connected Slack to our Python step.
The Slack access token is generated by Pipedream, and is available to this step in the pd.inputs[appName]["$auth"]
object:
from slack_sdk import WebClient
def handler(pd: "pipedream"):
token = pd.inputs["slack"]["$auth"]["oauth_access_token"]
# Authentication details for all of your apps are accessible under the special pd.inputs["slack"] variable:
console.log(pd.inputs["slack"]["$auth"])
pd.inputs["slack"]["$auth"]
contains named properties for each account you connect to the associated step. Here, we connected Slack, so this.slack.$auth
contains the Slack auth info (the oauth_access_token
).
The names of the properties for each connected account will differ with the account. Pipedream typically exposes OAuth access tokens as oauth_access_token
, and API keys under the property api_key
. But if there's a service-specific name for the tokens (for example, if the service calls it server_token
), we prefer that name, instead.
To list the pd.inputs["slack"]["$auth"]
properties available to you for a given app, just print the contents of the $auth
property:
print(pd.inputs["slack"]["$auth"]) # Replace "slack" with your app's name
and run your workflow. You'll see the property names in the logs below your step.
# Using the code templates tied to apps
When you write custom code that connects to an app, you can start with a code snippet Pipedream provides for each app. This is called the test request.
When you search for an app in a step:
- Click the + button below any step.
- Search for the app you're looking for and select it from the list.
- Select the option to Run Python with any [app] API.
This code operates as a template you can extend, and comes preconfigured with the connection to the target app and the code for authorizing requests to the API. You can modify this code however you'd like.
# Custom auth tokens / secrets
When you want to connect to a 3rd party service that isn't supported by Pipedream, you can store those secrets in Environment Variables.