# Simple security schemes

## Basic HTTP authentication

Basic HTTP authentication is supported in all languages.

Define `type: http` and `scheme: basic` to generate authentication that prompts users for a username and password when instantiating the SDK. The SDK will encode the username and password into a Base64 string and pass it in the `Authorization` header.

```yaml
paths:
  /drinks:
    get:
      operationId: listDrinks
      summary: Get a list of drinks.
      description: Get a list of drinks, if authenticated this will include stock levels and product codes otherwise it will only include public information.
      tags:
        - drinks
components:
  securitySchemes:
    auth:
      type: http
      scheme: basic
security:
  - auth: []
```

<CodeWithTabs
  tabs={[
    {
      label: "TypeScript",
      language: "typescript",
      code: `import { SDK } from "speakeasy";

  async function run() {
    const sdk = new SDK({
      security: {
        username: "<YOUR_USERNAME_HERE>",
        password: "<YOUR_PASSWORD_HERE>",
      },
    });

    const result = await sdk.drinks.listDrinks();

    // Handle the result
    console.log(result);
  }

  run();`,
    },
    {
      label: "Python",
      language: "python",
      code: `import speakeasy
  from speakeasy.models import components

  s = speakeasy.SDK(
      security=components.Security(
          username="<YOUR_USERNAME_HERE>",
          password="<YOUR_PASSWORD_HERE>",
      ),
  )

  res = s.drinks.list_drinks()

  if res.drinks is not None:
      # handle response
      pass`,
    },
    {
      label: "Go",
      language: "go",
      code: `package main

  public class Application {

      public static void main(String[] args) {
          try {
              SDK sdk = SDK.builder()
                  .security(Security.builder()
                      .username("<YOUR_USERNAME_HERE>")
                      .password("<YOUR_PASSWORD_HERE>")
                      .build())
                  .build();

              ListDrinksResponse res = sdk.drinks().listDrinks()
                  .call();

              if (res.drinks().isPresent()) {
                  // handle response
              }
          } catch (dev.speakeasyapi.speakeasy.models.errors.SDKError e) {
              // handle exception
          } catch (Exception e) {
              // handle exception
          }
      }
  }`,
    },
    {
      label: "C#",
      language: "csharp",
      code: `using Speakeasy;
  using Speakeasy.Models.Components;

  var sdk = new SDK(
      security: new Security()
      {
          Username = "<YOUR_USERNAME_HERE>",
          Password = "<YOUR_PASSWORD_HERE>",
      }
  );

  try
  {
      var res = await sdk.Drinks.ListDrinksAsync();

      if (res.Drinks != null)
      {
          // handle response
      }
  }
  catch (Exception ex)
  {
    // handle exception
  }`,
    },
    {
      label: "PHP",
      language: "php",
      code: `use OpenAPI\\OpenAPI;
  use OpenAPI\\OpenAPI\\Models\\Shared;

  $sdk = OpenAPI\\SDK::builder()->setSecurity(
      new Shared\\Security(
          password: 'YOUR_PASSWORD',
          username: 'YOUR_USERNAME',
    );
  )->build();

  try {
      $res = $sdk->drinks->listDrinks();

      if ($res->Drinks != null) {
          // handle response
      }
  } catch (Errors\\ErrorThrowable $e) {
      // handle exception
  }`,
    },
    {
      label: "Ruby",
      language: "ruby",
      code: `require 'openapi'

    Models = ::OpenApiSDK::Models

    s = ::OpenApiSDK::SDK.new(
      security: Models::Shared::Security.new(
        username: 'YOUR_USERNAME',
        password: 'YOUR_PASSWORD',
      ),
    )

    begin
      res = s.drinks.list_drinks

      unless res.drinks.nil?
        # handle response
      end
    rescue Models::Errors::APIError => e
      # handle exception
      raise e
    end
`,
}
]}
/>

## API key authentication

API key authentication is supported in all languages.

Define `type: apiKey` and `in: [header,query]` to generate authentication that prompts users for a key when instantiating the SDK. The SDK passes the key in a header or query parameter, depending on the `in` property, and uses the `name` field as the header or key name.

```yaml
paths:
  /drinks:
    get:
      operationId: listDrinks
      summary: Get a list of drinks.
      description: Get a list of drinks, if authenticated this will include stock levels and product codes otherwise it will only include public information.
      tags:
        - drinks
      responses:
        "200":
          description:
            OK
            #...
components:
  securitySchemes:
    api_key:
      type: apiKey
      name: api_key
      in: header
security:
  - api_key: []
```

<CodeWithTabs
  tabs={[
    {
      label: "TypeScript",
      language: "typescript",
      code: `import { SDK } from "speakeasy";

  async function run() {
    const sdk = new SDK({
      apiKey: "<YOUR_API_KEY_HERE>",
    });

    const result = await sdk.drinks.listDrinks();

    // Handle the result
    console.log(result);
  }

  run();`,
    },
    {
      label: "Python",
      language: "python",
      code: `import speakeasy

  s = speakeasy.SDK(
      api_key="<YOUR_API_KEY_HERE>",
  )

  res = s.drinks.list_drinks()

  if res.drinks is not None:
      # handle response
      pass`,
    },
    {
      label: "Go",
      language: "go",
      code: `package main

  public class Application {

      public static void main(String[] args) {
          try {
              SDK sdk = SDK.builder()
                  .apiKey("<YOUR_API_KEY_HERE>")
                  .build();

              ListDrinksResponse res = sdk.drinks().listDrinks()
                  .call();

              if (res.drinks().isPresent()) {
                  // handle response
              }
          } catch (dev.speakeasyapi.speakeasy.models.errors.SDKError e) {
              // handle exception
          } catch (Exception e) {
              // handle exception
          }
      }
  }`,
    },
    {
      label: "C#",
      language: "csharp",
      code: `using Speakeasy;
  using Speakeasy.Models.Components;

  var sdk = new SDK(
      security: new Security() { ApiKey = "<YOUR_API_KEY_HERE>" }
  );

  try
  {
      var res = await sdk.Drinks.ListDrinksAsync();

      if (res.Drinks != null)
      {
          // handle response
      }
  }
  catch (Exception ex)
  {
    // handle exception
  }`,
    },
    {
      label: "PHP",
      language: "php",
      code: `use OpenAPI\\OpenAPI;
  use OpenAPI\\OpenAPI\\Models\\Shared;

  $sdk = OpenAPI\\SDK::builder()->setSecurity(
      new Shared\\Security(
        apiKey: "<YOUR_API_KEY_HERE>"
    );
  )->build();

  try {
      $res = $sdk->drinks->listDrinks();

      if ($res->Drinks != null) {
          // handle response
      }
  } catch (Errors\\ErrorThrowable $e) {
    // handle exception
  }`,
    },
    {
      label: "Ruby",
      language: "ruby",
      code: `require 'openapi'

  Models = ::OpenApiSDK::Models

  s = ::OpenApiSDK::SDK.new(
    security: Models::Shared::Security.new(
      api_key: '<YOUR_API_KEY_HERE>',
    ),
  )

  begin
    res = s.drinks.list_drinks

    unless res.drinks.nil?
      # handle response
    end
  rescue Models::Errors::APIError => e
    # handle exception
    raise e
  end`,
}
]}
/>

## Bearer token authentication

Bearer token authentication is supported in all languages.

Define `type: http` and `scheme: bearer` to generate authentication that prompts users for a token when instantiating the SDK.
The SDK will pass the token in the `Authorization` header using the `Bearer` scheme, appending the `Bearer` prefix to the token if not already present.

```yaml
paths:
  /drinks:
    get:
      operationId: listDrinks
      summary: Get a list of drinks.
      description: Get a list of drinks, if authenticated this will include stock levels and product codes otherwise it will only include public information.
      tags:
        - drinks
components:
  securitySchemes:
    auth:
      type: http
      scheme: bearer
security:
  - auth: []
```

<CodeWithTabs
  tabs={[
    {
      label: "TypeScript",
      language: "typescript",
      code: `import { SDK } from "speakeasy";

  async function run() {
    const sdk = new SDK({
      auth: "<YOUR_BEARER_TOKEN_HERE>",
    });

    const result = await sdk.drinks.listDrinks();

    // Handle the result
    console.log(result);
  }

  run();`,
    },
    {
      label: "Python",
      language: "python",
      code: `import speakeasy

  s = speakeasy.SDK(
      auth="<YOUR_BEARER_TOKEN_HERE>",
  )

  res = s.drinks.list_drinks()

  if res.drinks is not None:
      # handle response
      pass`,
    },
    {
      label: "Go",
      language: "go",
      code: `package main

  public class Application {

      public static void main(String[] args) {
          try {
              SDK sdk = SDK.builder()
                  .auth("<YOUR_BEARER_TOKEN_HERE>")
                  .build();

              ListDrinksResponse res = sdk.drinks().listDrinks()
                  .call();

              if (res.drinks().isPresent()) {
                  // handle response
              }
          } catch (dev.speakeasyapi.speakeasy.models.errors.SDKError e) {
              // handle exception
          } catch (Exception e) {
              // handle exception
          }
      }
  }`,
    },
    {
      label: "C#",
      language: "csharp",
      code: `using Speakeasy;
  using Speakeasy.Models.Components;

  var sdk = new SDK(
      security: new Security() { Auth = "<YOUR_BEARER_TOKEN_HERE>" }
  );

  try
  {
      var res = await sdk.Drinks.ListDrinksAsync();

      if (res.Drinks != null)
      {
          // handle response
      }
  }
  catch (Exception ex)
  {
    // handle exception
  }`,
    },
    {
      label: "PHP",
      language: "php",
      code: `use OpenAPI\\OpenAPI;
  use OpenAPI\\OpenAPI\\Models\\Components;

  $sdk = OpenAPI\\SDK::builder()->setSecurity(
      new Components\\Security(
          auth: "<YOUR_BEARER_TOKEN_HERE>"
      );
  )->build();

  try {
      $res = $sdk->drinks->listDrinks();

      if ($res->Drinks != null) {
          // handle response
      }
  } catch (Errors\\ErrorThrowable $e) {
      // handle exception
  }`,
    },
    {
      label: "Ruby",
      language: "ruby",
      code: `require 'openapi'

    Models = ::OpenApiSDK::Models

    s = ::OpenApiSDK::SDK.new(
      security: Models::Shared::Security.new(
        auth: '<YOUR_BEARER_TOKEN_HERE>',
      ),
    )

    begin
      res = s.drinks.list_drinks

      unless res.drinks.nil?
        # handle response
      end
    rescue Models::Errors::APIError => e
      # handle exception
      raise e
    end`,
}
]}
/>
