> ## Documentation Index
> Fetch the complete documentation index at: https://docs.surge.app/llms.txt
> Use this file to discover all available pages before exploring further.

# Purchase phone number

> Purchase a new phone number for the account. You can specify search criteria or let the system select a random number.




## OpenAPI

````yaml POST /accounts/{account_id}/phone_numbers
openapi: 3.1.1
info:
  title: Surge
  version: '1.0'
servers:
  - url: https://api.surge.app
security:
  - authorization: []
paths:
  /accounts/{account_id}/phone_numbers:
    post:
      tags:
        - Phone numbers
      summary: Purchase a phone number
      description: >
        Purchase a new phone number for the account. You can specify search
        criteria or let the system select a random number.
      operationId: PurchasePhoneNumber
      parameters:
        - in: path
          name: account_id
          required: true
          schema:
            description: The account for which the phone number should be created.
            example: acct_01j9a43avnfqzbjfch6pygv1td
            type: string
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/PhoneNumberPurchaseParams'
        required: true
      responses:
        '201':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/PhoneNumber'
          description: Purchased phone number
        default:
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
          description: Error
      x-codeSamples:
        - lang: JavaScript
          source: >-
            import Surge from '@surgeapi/node';


            const client = new Surge({
              apiKey: process.env['SURGE_API_KEY'], // This is the default and can be omitted
            });


            const phoneNumber = await
            client.phoneNumbers.purchase('acct_01j9a43avnfqzbjfch6pygv1td');


            console.log(phoneNumber.id);
        - lang: Python
          source: |-
            import os
            from surge import Surge

            client = Surge(
                api_key=os.environ.get("SURGE_API_KEY"),  # This is the default and can be omitted
            )
            phone_number = client.phone_numbers.purchase(
                account_id="acct_01j9a43avnfqzbjfch6pygv1td",
            )
            print(phone_number.id)
        - lang: Go
          source: "package main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\n\t\"github.com/stainless-sdks/surge-go\"\n\t\"github.com/stainless-sdks/surge-go/option\"\n)\n\nfunc main() {\n\tclient := surge.NewClient(\n\t\toption.WithAPIKey(\"My API Key\"),\n\t)\n\tphoneNumber, err := client.PhoneNumbers.Purchase(\n\t\tcontext.TODO(),\n\t\t\"acct_01j9a43avnfqzbjfch6pygv1td\",\n\t\tsurge.PhoneNumberPurchaseParams{},\n\t)\n\tif err != nil {\n\t\tpanic(err.Error())\n\t}\n\tfmt.Printf(\"%+v\\n\", phoneNumber.ID)\n}\n"
        - lang: Ruby
          source: >-
            require "surge_api"


            surge = SurgeAPI::Client.new(api_key: "My API Key")


            phone_number =
            surge.phone_numbers.purchase("acct_01j9a43avnfqzbjfch6pygv1td")


            puts(phone_number)
        - lang: PHP
          source: >-
            <?php


            require_once dirname(__DIR__) . '/vendor/autoload.php';


            use Surge\Client;

            use Surge\Core\Exceptions\APIException;


            $client = new Client(apiKey: getenv('SURGE_API_KEY') ?: 'My API
            Key');


            try {
              $phoneNumber = $client->phoneNumbers->purchase(
                'acct_01j9a43avnfqzbjfch6pygv1td',
                areaCode: '801',
                latitude: 40.7128,
                longitude: -74.006,
                name: 'Support Line',
                type: 'local',
              );

              var_dump($phoneNumber);
            } catch (APIException $e) {
              echo $e->getMessage();
            }
components:
  schemas:
    PhoneNumberPurchaseParams:
      description: >
        Parameters for purchasing a new phone number. The system will
        automatically infer the type in certain cases:

        - If only area_code is provided, type is inferred from the area code

        - If only latitude/longitude are provided, type is inferred as 'local'

        - Otherwise, type must be explicitly specified
      examples:
        - description: Purchase any available local number
          summary: Random local number
          value:
            type: local
        - description: Purchase any available toll-free number
          summary: Random toll-free number
          value:
            type: toll_free
        - description: Purchase a number from area code 801
          summary: Number from area code
          value:
            area_code: '801'
        - description: Purchase a local number near New York City
          summary: Local number near coordinates
          value:
            latitude: 40.7128
            longitude: -74.006
      properties:
        area_code:
          description: >-
            The desired area code for this phone number. If provided without
            type, the type will be inferred.
          example: '801'
          pattern: ^\d{3}$
          type: string
        latitude:
          description: >-
            Latitude to search for nearby phone numbers. Must be used with
            longitude. If provided without type, type will be inferred as
            'local'.
          example: 40.7128
          maximum: 90
          minimum: -90
          type: number
        longitude:
          description: >-
            Longitude to search for nearby phone numbers. Must be used with
            latitude. If provided without type, type will be inferred as
            'local'.
          example: -74.006
          maximum: 180
          minimum: -180
          type: number
        name:
          description: >-
            A human-readable name for the phone number. If not provided,
            defaults to the formatted phone number.
          example: Support Line
          type: string
        type:
          description: >-
            Whether the phone number is local or toll-free. Can be omitted if
            area_code or latitude/longitude are provided.
          enum:
            - local
            - toll_free
          example: local
          type: string
      required: []
      title: PhoneNumberPurchaseParams
      type: object
    PhoneNumber:
      description: A phone number that can be used to send and receive messages and calls
      example:
        campaign_id: cpn_01jjnn7s0zfx5tdcsxjfy93et2
        id: pn_01jsjwe4d9fx3tpymgtg958d9w
        name: (801) 555-1234
        number: '+18015551234'
        type: local
      properties:
        campaign_id:
          description: >-
            The unique identifier of the campaign this phone number is attached
            to, if any
          type:
            - string
            - 'null'
        id:
          description: Unique identifier for the phone number
          type: string
        name:
          description: A human-readable name for the phone number
          type:
            - string
            - 'null'
        number:
          description: The phone number in E.164 format
          example: '+18015551234'
          format: phone-number
          pattern: ^\+[1-9]\d{6,14}$
          type: string
        type:
          description: Whether the phone number is local, toll-free, or short code
          enum:
            - local
            - short_code
            - toll_free
          type: string
      required:
        - id
        - name
        - number
        - type
        - campaign_id
      title: PhoneNumber
      type: object
    ErrorResponse:
      description: An error response
      example:
        error:
          message: The requested resource could not be found.
          type: not_found
      properties:
        error:
          $ref: '#/components/schemas/Error'
      required:
        - error
      title: ErrorResponse
      type: object
    Error:
      description: An error response
      example:
        message: The requested resource was not found.
        type: not_found
      properties:
        detail:
          additionalProperties: true
          description: Additional details about the error.
          type: object
        message:
          description: A human-readable error message.
          example: The requested resource was not found.
          type: string
        type:
          description: A unique error code.
          example: not_found
          type: string
      required:
        - type
        - message
      title: Error
      type: object
  securitySchemes:
    authorization:
      bearerFormat: Surge API token
      scheme: bearer
      type: http

````