Skip to content

apimatic/slackapi-dotnet-v2-sdk

Repository files navigation

Getting Started with SlackWebApi

One way to interact with the Slack platform is its HTTP RPC-based Web API, a collection of methods requiring OAuth 2.0-based user, bot, or workspace tokens blessed with related OAuth scopes.

Learn more about the Slack Web API: https://api.slack.com/web


Installation

Install this SDK into your application by adding a project reference to the SDK.

dotnet add reference <path-to-sdk>\SlackWebApi.csproj

Usage

using SlackWebApi;
using SlackWebApi.Core.ErrorResponse;
using SlackWebApi.Core.Exceptions;

string bearerToken = "1POdFZRZbvb...qqillRxMr2z";
SlackWebApiClient client = new SlackWebApiClient(new HttpClient(), new SlackWebApiClientOptions(), bearerToken);

try
{
    ConversationsCreateSuccessSchema response = await client.ConversationsCreate(
        token: null,
        name: "Greetings Channel",
        isPrivate: false);

    // TODO: decide what happens when api call is successfully completed
}
catch (SdkException<VoidErrorResponse> ex)
{
    // TODO: decide what happens when api call is resulted in an error status code
}

Client Options

The following options are configurable in your client:

Parameter Type Description
RetryOptions RetryOptions The Retry options for the API Calls

Retry Options

The following fields are available in RetryOptions:

Parameter Type Description
Timeout TimeSpan Per-request timeout; cancels requests exceeding this duration.
Default: 100s
StatusCodesToRetry IReadOnlyList<HttpStatusCode> HTTP status codes that trigger a retry.
Default: 408, 429, 500, 502, 503, 504
HttpMethodsToRetry IReadOnlyList<HttpMethod> HTTP methods eligible for retry.
Default: GET, HEAD, PUT, OPTIONS
MaxRetries int Maximum number of retry attempts.
Default: 3
Delay TimeSpan Base delay before each retry attempt.
Default: 1s
BackOffFactor int Multiplier for exponential backoff (e.g., 2 doubles each attempt's delay).
Default: 2
UseExponentialBackoff bool Enables exponential backoff; when false, uses constant delay.
Default: true
MaxJitter TimeSpan Maximum random jitter added to delay to reduce contention.
Default: 500ms
OnRetry Action<Exception, TimeSpan, int> Callback invoked on each retry with the error/result, applied delay, and attempt number.
Default: null

Retries and BackOff

using SlackWebApi;
using SlackWebApi.Models;

SlackWebApiClientOptions options =
    new SlackWebApiClientOptions
    {
        RetryOptions = SlackWebApi.Core.Configuration.RetryOptions.Default() with
        {
            MaxRetries = 5,
            Delay = TimeSpan.FromSeconds(2)
        }
    };

string bearerToken = "1POdFZRZbvb...qqillRxMr2z";

SlackWebApiClient client = new SlackWebApiClient(new HttpClient(), options, bearerToken);

// Make any API Call with HttpMethod GET
UsersListSchema response = await client.UsersList(token: null, limit:10, cursor: null, includeLocale: null);

Bearer Authentication

using SlackWebApi;
using SlackWebApi.Models;

// Initialize Client with bearer authentication
string bearerToken = "1POdFZRZbvb...qqillRxMr2z";
SlackWebApiClient client = new SlackWebApiClient(new HttpClient(), new SlackWebApiClientOptions(), bearerToken);


// Make an API Call that requires bearer authentication
UsersListSchema response = await client.UsersList(token: null, limit:10, cursor: null, includeLocale: null);

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages